我正在尝试这样做,每当我的页面“test.php”加载时,只要用户点击我的功能就会启动#start
当用户离开广告(例如到另一个选项卡)时,该功能将停止。(在职的)
每当用户返回选项卡 (test.php) 时,该函数应该重新启动。我似乎找不到该怎么做..目前我有这个:
$("body").on('click', '#start', function()
{
$('#website').focus();
$('.message').html('<div class="alert info"><b id="seconds">'+parseFloat($seconds-$current_second)+'</b> seconds remaining.</div>');
if($timer !== null) return;
$timer = setInterval(function()
{
if($current_second==$seconds)
{
clearInterval($timer);
$('body').addClass('done');
$('.message').html('<div class="alert info">Validating advertisement…</div>');
$.post('index.php?i=v&p=k&token=' + token,{'key': key,'token':token,'time':time,'stime':stime},
function (data) {
proccessData(data);
});
return false;
}
else
{
var $counter_bar_width = $('#bar').innerWidth();
//$('#bar').css('width',parseFloat($counter_bar_width+$width_per_second).toFixed(2));
var left = (<?php echo $time; ?>-$current_second)*1000;
$current_second++;
$("#seconds").text(parseFloat($seconds-$current_second));
$('#bar').animate({
width:'100%'
}, left);
}
},1000);
});
var vis = (function(){
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();
var handler = function(){
// calling vis() with no arguments will return a boolean
if (vis()) {
//Visible.
Page tab is now visible. Now the timer and the bar should start moving / counting down again.
} else {
//Not visible.
var myDiv = $("#bar");
myDiv.clearQueue();
myDiv.stop();
clearInterval($timer);
$timer = null
}
}
// if a handler is passed, it gets bound to the event
vis(handler);
});