简体   繁体   English

setInterval()-但仅运行一次

[英]setInterval() - but run it only once

If I call the update function several times I get multiple instances of this interval running. 如果我多次调用更新函数,则会运行此间隔的多个实例。 How can I be sure that this only gets triggered once per idVar? 如何确定每个idVar仅触发一次?

For example if I run this twice in a row with the same idVar I get two updates per second, where it should only be one. 例如,如果我使用相同的idVar连续运行两次,则我每秒获得两次更新,而更新一次应该只有一个。

If the idVar is different it should run multiple times. 如果idVar不同,则应多次运行。 idVar is used in Update(). idVar用于Update()。

intervals[idVar] = setInterval(function() {
    update();                            
}, 1000);
intervals[idVar] = setTimeout(function() {
    update();                            
}, 1000);

If you really need to use setInterval and dont want to use setTimeout , you can clear it when you enter function 如果您确实需要使用setInterval而又不想使用setTimeout,则可以在输入函数时将其清除

For example 例如

var interval

interval = setInterval(function(){
   clearInterval(interval)
  //Your code
});

If you really want a new instance of the interval running for each unique idVar, you just need to check to see if you already have an instance of the interval running for the current idVar: 如果您确实想要为每个唯一的idVar运行一个新的间隔实例,则只需检查一下是否已经有一个为当前idVar运行的间隔实例:

if (!intervals[idVar]) {
    intervals[idVar] = setInterval(function() {
        update();                            
    }, 1000);
}

setInterval returns a timeoutID that is used to identify it. setInterval返回用于标识它的timeoutID

To stop a setInterval you pass it's timoueID to clearInterval . 要停止setInterval请将其timoueID传递给clearInterval


Explanation of why you get two intervals running 解释为什么要运行两个间隔

When you do this: 执行此操作时:

 intervals[idVar] = setInterval(function() { update(); }, 1000); intervals[idVar] = setInterval(function() { update(); }, 1000); 

What you are doing is: 您正在做的是:

  • call your first setInterval and setting intervals[idVar] to this first setInterval's timeoutID 调用您的第一个 setInterval并将intervals[idVar]设置为此第一个setInterval's timeoutID

  • call your second setInterval and overwrite the value stored in intervals[idVar] with the timeoutID of your newly called setInterval 调用第二个setInterval 调用的setInterval的timeoutID 覆盖存储在intervals[idVar]的值

Result : Two seperate intervals, with intervals[idVar] storing the timeoutID of the second one called 结果 :两个单独的间隔,其中intervals[idVar]存储第二个被调用的timeoutID


setTimeout 的setTimeout

setTimeout is like setInterval and actually uses the same pool of timeout IDs, but it is only invoked once after the specified delay. setTimeout类似于setInterval,实际上使用相同的超时ID池,但仅在指定的延迟后调用一次。


Setting one interval (setTimeout or setInterval) per idVar 每个idVar设置一个间隔(setTimeout或setInterval)

 var idVar = setInterval(function() { update(); }, 1000); // sets idVar to the timeoutID and starts the interval } intervals[idVar] = idVar; // assigns a property 'idVar' on `intervals` // and sets the timeoutID as its value // allows you to call clearInterval(intervals[idVar]); 

However, without knowing exactly what your update() method is doing it is difficult to tell what the best solution would be here... 但是,如果不确切知道您的update()方法在做什么,那么很难说出最佳解决方案是什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM