简体   繁体   English

JavaScript setInterval()方法无法正常运行

[英]JavaScript setInterval() method not working as I expect it to

I'm having an array of errors string, these errors are errors in a form after submitting. 我有一个错误字符串数组,这些错误是提交后表单中的错误。

I want to display these error as notification one by one after a fixed time, let say every 5 seconds. 我想将这些错误显示为固定时间后的通知,每5秒说一次。

The code that I have is: 我拥有的代码是:

 var array = ["1", "2", "3", "4", "5", "6"]; recursive(); function recursive() { var error = array.pop(); if (typeof error === 'undefined') { return; } else { console.log(error); // showNoti(error); } setInterval(function() { recursive() }, 5000); } 

The problem with this code is that it's running fine in the first 2 iterations, printing out 5 after 5 seconds printing 6. 此代码的问题在于,它在前两个迭代中运行良好,在打印5秒钟后打印了5则打印出了5。

But at the third iteration, it prints both string 4 and 3. Similar to the fourth iteration. 但是在第三次迭代中,它同时打印字符串4和3。类似于第四次迭代。

How can I fix it so that it print each element in the array in every 5 seconds? 如何修复它,以便每5秒打印一次数组中的每个元素?

Currently, each call of recursive is initializing another interval. 当前,每次recursive都在初始化另一个间隔。 So, for example, after the first call, there will then be one interval running: after the second call, another interval will be initialized (two intervals), etc. 因此,例如,在第一次调用之后,将运行一个间隔:在第二次调用之后,将初始化另一个间隔(两个间隔),依此类推。

Put the setInterval outside the recursive instead: setInterval放在recursive之外:

 var array = ["1", "2", "3", "4", "5", "6"]; recursive(); function recursive() { var error = array.pop(); if (typeof error === 'undefined') { return; } else { console.log(error); // showNoti(error); } } setInterval(function() { recursive() }, 500); 

Or use setTimeout instead: 或者使用setTimeout代替:

 var array = ["1", "2", "3", "4", "5", "6"]; recursive(); function recursive() { var error = array.pop(); if (typeof error === 'undefined') { return; } else { console.log(error); // showNoti(error); } setTimeout(recursive, 500); } 

Move the setInterval outside of recursive . setInterval recursive之外。 Every time you run recursive , you are creating a new setInterval 每次运行recursive ,您都在创建一个新的setInterval

 var array = ["1", "2", "3", "4", "5", "6"]; recursive(); function recursive() { var error = array.pop(); if (typeof error === 'undefined') { return; } else { console.log(error); // showNoti(error); } } setInterval(recursive, 5000); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

On all recursive calls, new setInterval is initialised. 在所有递归调用上,都会初始化新的setInterval。 Move it out of recursive function or use setTimeout , 将其移出递归函数或使用setTimeout

 var array = ["1", "2", "3", "4", "5", "6"]; recursive(); function recursive() { var error = array.pop(); if (typeof error === 'undefined') { return; } else { console.log(error); // showNoti(error); } } setInterval(function() { recursive() }, 5000); 

setInterval will start an infinitely repeating timer. setInterval将启动一个无限重复的计时器。 In this case you are controlling whether you want it to repeat or not within your code. 在这种情况下,您可以控制是否要在代码中重复。

That's what 'setTimeout' is for - a single delayed execution. 这就是'setTimeout'的目的-一个延迟的执行。

Change setInterval to setTimeout setInterval更改为setTimeout

var array = ["1", "2", "3", "4", "5", "6"];
recursive();

function recursive() {
  var error = array.pop();
  if (typeof error === 'undefined') {
    return;
  } 
  console.log(error);
  setTimeout(function() {
    recursive()
  }, 5000);
}

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

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