在JavaScript中,如何从其事件函数中访问setTimeout / setInterval调用的id? [英] In JavaScript, how can I access the id of setTimeout/setInterval call from inside its event function?
本文介绍了在JavaScript中,如何从其事件函数中访问setTimeout / setInterval调用的id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从其事件函数中访问setTimeout / setInterval调用的进程id,因为Java线程可能会访问自己的线程ID?
How can I access the process id of setTimeout/setInterval call from inside its event function, as a Java thread might access its own thread id?
var id = setTimeout(function(){
console.log(id); //Here
}, 1000);
console.log(id);
推荐答案
该代码将按原样运行,因为 setTimeout
在调用提供的回调之前始终将返回,即使您传递的超时值非常小,为零或为负。
That code will work as-is, since setTimeout
will always return before invoking the provided callback, even if you pass a timeout value which is very small, zero, or negative.
> var id = setTimeout(function(){
console.log(id);
}, 1);
undefined
162
> var id = setTimeout(function(){
console.log(id);
}, 0);
undefined
163
> var id = setTimeout(function(){
console.log(id);
}, -100);
undefined
485
问题是我的计划有许多同时安排的匿名操作,因此他们无法从同一个变量加载他们的id。
Problem is I plan to have many concurrently scheduled anonymous actions, so they can't load their id from the same variable.
当然可以。
(function () {
var id = setTimeout(function(){
console.log(id);
}, 100);
})();
这篇关于在JavaScript中,如何从其事件函数中访问setTimeout / setInterval调用的id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文