如何更新setTimeout当前时间?
例如如果我有
setTimeout(function(){
$( idleTimeout ).dialog("open");
}, 20000);
然后在5秒后我想告诉计时器只剩下10秒而不是15秒。在这种情况下,基本上跳过了5秒。
谢谢
您可以通过清除原始超时并创建新超时来更新setTimeout函数。
// desired code to execute after timeout
var code = function() {
$( idleTimeout ).dialog("open");
};
// originally set to occur after 20 secods
var timeoutID = window.setTimeout(code, 20000);
// after 5 seconds update to run in another 10 seconds
window.setTimeout(function() {
window.clearTimeout(timeoutID);
window.setTimeout(code, 10000);
}, 5000);
// with a bit of tweak it work. change setTimeout to setInterval
var timeoutID = window.setInterval(code, 10000);
window.setTimeout(function() {
window.clearInterval(timeoutID);
window.setInterval(code, 500);
}, 20000);