重置setTimeout [英] Resetting a setTimeout

查看:97
本文介绍了重置setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

window.setTimeout(function() {
    window.location.href = 'file.php';
}, 115000);

如何通过.click功能在倒计时中途重置计数器?

How can I, via a .click function, reset the counter midway through the countdown?

推荐答案

您可以存储对该超时的引用,然后调用 clearTimeout

You can store a reference to that timeout, and then call clearTimeout on that reference.

// 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(...);

这篇关于重置setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆