Node.js 设置forloop内部超时?JS节点

Node.js 设置forloop内部超时?JS节点,node.js,for-loop,delay,settimeout,Node.js,For Loop,Delay,Settimeout,我试图在每个循环之间创建一个延迟。然而,在循环被触发之前,我总是以延迟结束。我的做法如下: for (var i = 0; i < items.length; i++) { setTimeout(function() { console.log(i + ". " + items[i]['name']); priceManagement.FindPrices(items[i]['name']); }, 300

我试图在每个循环之间创建一个延迟。然而,在循环被触发之前,我总是以延迟结束。我的做法如下:

    for (var i = 0; i < items.length; i++)
    {
      setTimeout(function()
      {
        console.log(i + ". " + items[i]['name']);
        priceManagement.FindPrices(items[i]['name']);
      }, 3000);
    }
for(变量i=0;i
您可以执行以下操作:

 for (var i = 0; i < items.length; i++)
    {
      setTimeout(function(i)
      {
        console.log(i + ". " + items[i]['name']);
        priceManagement.FindPrices(items[i]['name']);
      }, 3000*i, i);
    }
我建议您阅读以下内容:

您可以这样做:

 for (var i = 0; i < items.length; i++)
    {
      setTimeout(function(i)
      {
        console.log(i + ". " + items[i]['name']);
        priceManagement.FindPrices(items[i]['name']);
      }, 3000*i, i);
    }

我建议您阅读以下内容:

您的主循环正在连续执行
setTimeout
函数。由于此函数的异步特性,您将期望看到函数在它们之间无延迟地执行

当函数的执行时间远小于延迟时间时,如果想要获得近似效果,可以使用
setInterval
。这将在亚当的安瓦尔身上实现同样的效果

但对于确切的“在每个循环之间创建延迟”,您将需要一些回调

首先想到的是:

function main() {
  doWork(items, 0, items.length);
}

function doWork(items, i, loopLength) {
  // The bit in the loop
  console.log(i + ". " + items[i]['name']);
  priceManagement.FindPrices(items[i]['name']);
  i++;

  if (i < loopLength) {
    delayDoWork(items, i, loopLength);
  }
  else {
    // Do rest in the main
    ...
  }
}

function delayDoWork(items, i, loopLength) {
  setTimeout(function(items, i, loopLength)
  {
    doWork(items, i, loopLength);
  }, 3000, items, i, loopLength);
}
函数main(){
销钉(项目,0,项目.长度);
}
功能定位(项目、i、环长){
//循环中的位
log(i+“+”项[i]['name']);
priceManagement.FindPrices(items[i]['name']);
i++;
if(i
这将保证循环之间的精确延迟

编辑

你可能想对此做更多的实验,因为我不是一个知道
setInterval
如何在JS中工作的专家,因为它本质上不是多线程的?起点将考虑以下对此的报价

实际上,setTimeout()会在执行队列的末尾对新JavaScript重新排队


主循环正在连续执行
setTimeout
函数。由于此函数的异步特性,您将期望看到函数在它们之间无延迟地执行

当函数的执行时间远小于延迟时间时,如果想要获得近似效果,可以使用
setInterval
。这将在亚当的安瓦尔身上实现同样的效果

但对于确切的“在每个循环之间创建延迟”,您将需要一些回调

首先想到的是:

function main() {
  doWork(items, 0, items.length);
}

function doWork(items, i, loopLength) {
  // The bit in the loop
  console.log(i + ". " + items[i]['name']);
  priceManagement.FindPrices(items[i]['name']);
  i++;

  if (i < loopLength) {
    delayDoWork(items, i, loopLength);
  }
  else {
    // Do rest in the main
    ...
  }
}

function delayDoWork(items, i, loopLength) {
  setTimeout(function(items, i, loopLength)
  {
    doWork(items, i, loopLength);
  }, 3000, items, i, loopLength);
}
函数main(){
销钉(项目,0,项目.长度);
}
功能定位(项目、i、环长){
//循环中的位
log(i+“+”项[i]['name']);
priceManagement.FindPrices(items[i]['name']);
i++;
if(i
这将保证循环之间的精确延迟

编辑

你可能想对此做更多的实验,因为我不是一个知道
setInterval
如何在JS中工作的专家,因为它本质上不是多线程的?起点将考虑以下对此的报价

实际上,setTimeout()会在执行队列的末尾对新JavaScript重新排队


它有点工作,但是它在最后一个整数上循环,并且它不能到达'items'变量的内容。这正是我需要的。非常感谢。它有点工作,但是它在最后一个整数上循环,并且它不能到达'items'变量的内容。这正是我需要的。非常感谢。对,那是另一种方式,我知道这一点。然而,它需要更多的代码,我很好奇为什么它不能像我预期的那样工作,因为在不同的编程语言中,我从来没有遇到过类似的问题。谢谢你的解释!我也不是一个完全的JS人。干杯:)对,那是另一种方式,我知道这一点。然而,它需要更多的代码,我很好奇为什么它不能像我预期的那样工作,因为在不同的编程语言中,我从来没有遇到过类似的问题。谢谢你的解释!我也不是一个完全的JS人。干杯:)