[英]force a javascript timeout function to execute immediately
If I have set a timeout in javascript如果我在 javascript 中设置了超时
var myTimeoutId = setTimeout( function(){
... do some stuff ....
}, 15000);
and a different event makes me need to have that timeout execute immediate, is there a way that I can use that stored id to execute the function?并且另一个事件使我需要立即执行该超时,有没有办法可以使用该存储的 id 来执行该函数? I know that I could store off a reference to that function, call
clearTimeout
on myTimeoutId
and then call the function directly, but there's a decent amount of book keeping involved in that.我知道我可以存储对该函数的引用,在
myTimeoutId
上调用clearTimeout
然后直接调用该函数,但是这涉及到大量的簿记。
Is there a function along the lines of executeNow( myTimeoutId )
?是否有类似
executeNow( myTimeoutId )
的函数?
No. You need to create the function separately so it can be called from anywhere:不。您需要单独创建该函数,以便可以从任何地方调用它:
function fn (){
// ... do some stuff ....
}
var myTimeoutId = setTimeout( fn, 15000);
// one possibility
function other () {
clearTimeout(myTimeoutId);
fn();
}
in nodejs:在 nodejs 中:
const timeout = setTimeout(() => {
console.log("timeout function");
}, 10000);
// execute timeout function
timeout._onTimeout();
clearTimeout(timeout);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.