setTimeout in Node.js loop

后端 未结 8 1622
眼角桃花
眼角桃花 2020-12-06 05:16

I\'m a bit confused as to how setTimeout works. I\'m trying to have a setTimeout in a loop, so that the loop iterations are, say, 1s apart. Each l

8条回答
  •  没有蜡笔的小新
    2020-12-06 06:09

    I'm surprised that no one has mentioned this above, but it sounds like you need setInterval not setTimeout.

    vat poller = setInterval(makeRequestFunc, 3000)
    

    The code above will make a request every 3 seconds. Since you saved the object to the variable poller, you can stop polling by clearing the object like so:

    cleanInterval(poller)
    

提交回复
热议问题