search

Home  >  Q&A  >  body text

node.js - nodejs如何同步延迟运行函数

队列示例

var array = [1,2,3,4];
function check()
{
if(array.length > 0)
var url = array.pop();
load(url);
}}
function load(url)
{
http.get(url, function(res){
check();
)
}}

我想在 每次load(url)的时候 延迟6秒 这时候我用setTimeout 没什么效果

PHP中文网PHP中文网2786 days ago423

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 15:56:35

    Use sleep

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:56:35

    1. It’s best to format it when sending the code! Dear

    2. If you use setTimeout to implement it, you need to recurse

    reply
    0
  • PHPz

    PHPz2017-04-17 15:56:35

    The timing function can be written like this

        var a = function(){
            //逻辑
            setTimeout(() => {
                a()
            }, time)
        }

    I don’t know if that’s what you want

    reply
    0
  • Cancelreply