0
我試圖做的,每當我的頁面「test.php的」負載,然後我的函數將每當用戶點擊在#start
啓動功能再次自動
當用戶從廣告導航離開(另一個開始例如),然後該功能將停止。 (working)
每當用戶回到選項卡(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);
});
是否有一個特定的原因,你是用'$'作爲變量的前綴? – Ryan
不是。我對jQuery並不熟悉,只是在網上看到它。對我有意義。 – oliverbj
當你將jQuery對象作爲提示時,它有點合理。但不是每個變量。例如:'var $ jqElement = $('#someElement');'相反'var ordinaryVar = 0;'。 –