Nodejs 在循环中等待 [英] Nodejs wait in a Loop

查看:82
本文介绍了Nodejs 在循环中等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环等待,实现此目的的最佳方法是什么?

I would like to wait in a loop, what is the best way to achieve this ?

这是我的实际代码:

var groups = ['461', '6726', '3284', '4', '121', '11', '399', '1735', '17', '19', '1614 ];
groups.forEach(function (value) {
            myfunction(value);
        });

我希望每 5 分钟调用一次 myfunction().

I would like that myfunction() being called each 5 minutes.

我想遍历组数组一次并在每个项目之间等待直到读取结束

I would like to loop through the groups array once and waiting between each item until the end is read

最好的方法是什么?

推荐答案

这是一个使用 setTimeout() 的简单解决方案:

Here's a simple solution using setTimeout():

var groups = ['461', '6726', '3284', '4', '121', '11', '399', '1735', '17', '19', '1614'];
function nextGroup(idx) {
  idx = idx || 0;
  if (idx < groups.length) {
    myfunction(groups[idx]);
    setTimeout(function() {
      nextGroup(idx + 1);
    }, 5 * 60 * 1000);
  }
}
nextGroup();

这篇关于Nodejs 在循环中等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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