闭包中的SetInterval在For循环内创建了太多的间隔,不会停止运行 [英] SetInterval inside a Closure, inside a For loop, creates too many intervals, won't stop running

查看:267
本文介绍了闭包中的SetInterval在For循环内创建了太多的间隔,不会停止运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var myArray = []; 
var tempInterval = 0;

//运行6分钟
(i = 0; i <360; i ++){

//关闭以使'i'正确setInterval运行
tempInterval =(function(i){

interval1 = setInterval(function(){
console.log(i);
},( i * 1000));

return interval1;

})(i);

//将setInterval值放入一个数组
myArray.push(tempInterval);

我使用 Closure 因为我需要 i 的值为 0,1,2,... etc ,如果你不要使用闭包,到时间间隔执行的时候就是360.



如果你在控制台中运行它,你会发现它运行得更多比360倍。出于某种原因,它创造了太多的时间间隔。

我想有一个函数来停止这个setIntervals的执行:



(i = 0; i< myArray.length; i ++){
clearInterval(myArray [i]);

  

$ / code $ / pre

但是,因为它创造太多,我不能停止它。
$ b

例如,如果你在中为(i = 0; i <5; i ++)运行它,

(i = 0; i <360; i ++)为什么它创造了这么多的时间间隔,我怎样才能让它只运行360次呢?另外,这是保存间隔的正确方法,所以我可以使用 clearInterval 来停止它们的执行吗?

解决方案

setInterval 意思是睡眠,意思是每一段时间都运行这个函数。

每循环360次,都会调用每一个 i 秒。



setInterval
所以你360倒计时,每一个运行一个函数,当它打到零之前,重新开始倒计时再次运行该功能。






解决方案



不要使用 setInterval 。 c $ c> setTimeout



或者:

一般情况下,应避免设置360个计时器,而应该这样做:

  var i = 0; 

函数next(){
console.log(i);
i ++;
if(i< 360){
setTimeout(next,1000);
}
}

next();


I've got some rather complicated logic:

var myArray = [];
var tempInterval = 0;

// Run for 6 minutes
for(i=0;i<360;i++) {

  // Closure so that 'i' is the right value when setInterval runs
  tempInterval = (function(i) {

      interval1 = setInterval(function() {
          console.log(i);
      }, (i * 1000));

      return interval1; 

  })(i);

  // Put the setInterval value into an array
  myArray.push(tempInterval);
}

I use a Closure because I need the value of i to be 0,1,2,...etc, and if you don't use a closure, it'll be 360 by the time the interval executes.

If you run that in your console, you'll see it runs way more than 360 times. For some reason, it's creating too many intervals.

I want to have a function to stop this execution of setIntervals:

for(i=0; i<myArray.length; i++) {
    clearInterval(myArray[i]);
}

But, because it creates too many, I can't get it to stop.

For example, if you run it inside for(i=0; i<5; i++) instead of for(i=0;i<360;i++), you'll see, it still runs indefinitely.

Why is it creating so many intervals, and how do I get it to run only 360 times? Also, is that the right way to save the intervals, so I can use clearInterval to stop their execution?

解决方案

The problem

setInterval does not mean "sleep", it means "Run this function every time period".

Each of the 360 times you go around the loop, you call setInterval saying "Run this function every i seconds.

So you get 360 countdowns, each of which runs a function when it hits zero before restarting the countdown to run the function again.


The solution

Don't use setInterval. Use setTimeout.

Or, alternatively:

I'd generally avoid setting 360 timers running at once and instead do something like:

var i = 0;

function next() {
    console.log(i);
    i++;
    if (i < 360) {
        setTimeout(next, 1000);
    }
}

next();

这篇关于闭包中的SetInterval在For循环内创建了太多的间隔,不会停止运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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