我有以下几点:
window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
如何通过.click功能,在倒计时中途重置计数器?
我有以下几点:
window.setTimeout(function() {
window.location.href = 'file.php';
}, 115000);
如何通过.click功能,在倒计时中途重置计数器?
当前回答
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>
您可以存储对该超时的引用,然后在该引用上调用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(...);
var myTimer = setTimeout(..., 115000);
something.click(function () {
clearTimeout(myTimer);
myTimer = setTimeout(..., 115000);
});
沿着这条线!
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方法。
$(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])} );
})();
});