如何使用循环索引保存/清除的setTimeout的阵列? [英] How to save/clear setTimeout's array using loop's index?

查看:153
本文介绍了如何使用循环索引保存/清除的setTimeout的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话一个循环多次。我想保存单个的setTimeout每个索引。我们的想法是使用循环的指数的setTimeout的数组索引,但的setTimeout返回一个增量的ID号,我想重​​置,或者重写拿回来ID的控制权,这样我就可以识别每个暂停时以给定的指标,始终在执行一遍一遍回路的索引的范围内。能够与一个特定的索引以后使用clearTimeout

VAR超时= [];
为(变量I = 0; I&小于5;我++){
   ...
   (功能(延时,超时我){
      //保持超时的参考稍后将其清除。
      超时[I] ​​= setTimeout的(函数(){
         countInView--;
      },延迟);
      的console.log(我:+5+|超时:+超时[I]);
      //我:5 |超时:25 - >赶超指数,
      //我想用ID或索引覆盖位置。
   }(延迟,超时一));
   ...
}


  1. 在该循环被执行一次以上,在超时[I] ​​值超过环路的指数值:


  2. 我只是想以清除code的另一部分,但超时到达code的这一部分时,超时[I] ​​<值/ code>可能是140,和环路 I 价值只有3,所以我从来没有可以清除3日(或任何)setTimeout的打算保存:

    clearTimeout(超时[I]);



解决方案

您可以使用2维数组这个,第一个维度将与一起改变循环索引,而第二个方面能够保持在0采取分配的控制超时的标识为每个迭代。

对于一个循环索引中保存的setTimeout

VAR超时= []; //二维阵列
对于(I = 0; I&小于5;我+ +)
    超时[I] ​​= [];为(变量I = 0; I&小于5;我++){
    ...
    (函数(延迟,$元素,savedtimeout){
        savedtimeout [0] = setTimeout的(函数(){
            countInView--;
        },延迟,savedtimeout);
    }(延迟,$元,超时[I]));
    ...
}

对于一个循环索引中明确的setTimeout

如果(超时[I] ​​[0]!= NULL){
    //删除从队列的超时
    clearTimeout(超时[I] ​​[0]);
}

I am calling a for loop multiple times. I would like to save a single setTimeout for each index. The idea is to use the loop's index as setTimeout's array index, but setTimeout returns an incremental ID number, I would like to reset it, or override to take control of the returning ID, so I can identify each timeout with a given index, always within the range of loop's index that is executed over and over again. Being able to use clearTimeout later on with a specific index.

var timeouts = [];
for(var i=0; i < 5 ; i++) {
   ...
   (function(delay, timeouts, i){
      // Keeps a reference of the timeout to clear it later.
      timeouts[i] = setTimeout(function() {
         countInView--;
      }, delay);
      console.log("i: "+5+ " | timeout: "+timeouts[i]);
      // i: 5 | timeout: 25 -> Surpasses index, 
      // I'd like to override positions by ID or by index.
   }(delay, timeouts, i));
   ...
}

  1. When the loop is executed more than once, the timeouts[i] value surpasses the value of loop's index:

  2. I just want to clear the timeout in other part of the code, but when reaching this part of the code, the value of timeouts[i] may be 140, and loop i value only 3, so I never can clear the 3rd(nor any) setTimeout intended to be saved:

    clearTimeout(timeouts[i]);
    

解决方案

You could use 2 dimensional array for this, first dimension will change along with the for loop index, whilst second dimension could remain in 0 to take control of the assigned timeout's IDs for each iteration.

For saving setTimeout within a index in a loop

var timeouts = []; // Two  dimensional array
for (i = 0; i < 5; i++)  
    timeouts[i] = [];

for(var i=0; i < 5 ; i++) {
    ...
    (function(delay, $element, savedtimeout){
        savedtimeout[0] = setTimeout(function() {
            countInView--;
        }, delay, savedtimeout);
    }(delay, $element, timeouts[i]));
    ...
}

For clear setTimeout within a index in a loop

if(timeouts[i][0] != null) {
    //Removes the timeout from the queue
    clearTimeout(timeouts[i][0]);
}

这篇关于如何使用循环索引保存/清除的setTimeout的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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