我有以下内容:
window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
如何通过.click函数在倒计时中途重置计数器?
您可以存储对该超时的引用,然后调用clearTimeout
该引用。
// in the example above, assign the result
var timeoutHandle = window.setTimeout(...);
// in your click function, call clearTimeout
window.clearTimeout(timeoutHandle);
// then call setTimeout again to reset the timer
timeoutHandle = window.setTimeout(...);
clearTimeout()并提供setTimeout的引用,该引用将是一个数字。然后重新调用它:
var initial;
function invocation() {
alert('invoked')
initial = window.setTimeout(
function() {
document.body.style.backgroundColor = 'black'
}, 5000);
}
invocation();
document.body.onclick = function() {
alert('stopped')
clearTimeout( initial )
// re-invoke invocation()
}
在此示例中,如果5秒钟内未单击body元素,则背景色将为黑色。
参考:
- https://developer.mozilla.org/en/DOM/window.clearTimeout
- https://developer.mozilla.org/En/Window.setTimeout
注意:setTimeout和clearTimeout不是ECMAScript本机方法,而是全局窗口名称空间的Javascript方法。
您将必须记住超时“ Timer”,将其取消,然后重新启动:
g_timer = null;
$(document).ready(function() {
startTimer();
});
function startTimer() {
g_timer = window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
}
function onClick() {
clearTimeout(g_timer);
startTimer();
}
var myTimer = setTimeout(..., 115000);
something.click(function () {
clearTimeout(myTimer);
myTimer = setTimeout(..., 115000);
});
遵循这些原则!
30秒后,此计时器将触发“ Hello”警报框。但是,每次单击重置计时器按钮时,都会清除timerHandle,然后再次将其重新设置。发射后,游戏结束。
<script type="text/javascript">
var timerHandle = setTimeout("alert('Hello')",3000);
function resetTimer() {
window.clearTimeout(timerHandle);
timerHandle = setTimeout("alert('Hello')",3000);
}
</script>
<body>
<button onclick="resetTimer()">Reset Timer</button>
</body>
var redirectionDelay;
function startRedirectionDelay(){
redirectionDelay = setTimeout(redirect, 115000);
}
function resetRedirectionDelay(){
clearTimeout(redirectionDelay);
}
function redirect(){
location.href = 'file.php';
}
// in your click >> fire those
resetRedirectionDelay();
startRedirectionDelay();
这是一个详细说明http://jsfiddle.net/ppjrnd2L/
$(function() {
(function(){
var pthis = this;
this.mseg = 115000;
this.href = 'file.php'
this.setTimer = function() {
return (window.setTimeout( function() {window.location.href = this.href;}, this.mseg));
};
this.timer = pthis.setTimer();
this.clear = function(ref) { clearTimeout(ref.timer); ref.setTimer(); };
$(window.document).click( function(){pthis.clear.apply(pthis, [pthis])} );
})();
});
要重置计时器,您需要设置并清除计时器变量
$time_out_handle = 0;
window.clearTimeout($time_out_handle);
$time_out_handle = window.setTimeout( function(){---}, 60000 );
我知道这是一个老话题,但是我今天想出了这个
var timer = []; //creates a empty array called timer to store timer instances
var afterTimer = function(timerName, interval, callback){
window.clearTimeout(timer[timerName]); //clear the named timer if exists
timer[timerName] = window.setTimeout(function(){ //creates a new named timer
callback(); //executes your callback code after timer finished
},interval); //sets the timer timer
}
然后您使用
afterTimer('<timername>string', <interval in milliseconds>int, function(){
your code here
});
本文地址:http://javascript.askforanswer.com/zhongzhisettimeout.html
文章标签:javascript , jquery
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:javascript , jquery
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!