延迟节点js中的每个循环迭代,异步 [英] Delay each loop iteration in node js, async

查看:87
本文介绍了延迟节点js中的每个循环迭代,异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码:

  var request = require('request'); 
var cheerio = require(cheerio);
var async = require(async);

var MyLink =www.mylink.com;
$ b $ async.series([

function(callback){
request(Mylink,function(error,response,body){
if(error )返回回调(错误);
var $ = cheerio.load(body);
//一些计算,我得到NewUrl变量...
TheUrl = NewUrl;
callback ();
});
},
function(callback){
for(var i = 0; i< = TheUrl.length-1; i ++){
var url ='www.myurl.com ='+ TheUrl [i];
request(url,function(error,resp,body){
if(error)return callback(error) ;
var $ = cheerio.load(body);
//一些计算再次...
callback();
});
}; $ b (错误);
$ b],函数(错误){
if(error)return next(error);
});

有没有人有关于如何延迟每个循环迭代 for循环?假设代码在每次迭代完成后等待10秒钟。我尝试了 setTimeout 但是没有管理它。

解决方案

你可以像下面这样增加间隔来设置代码的执行时间:

$ p $ var $ interval = 10 * 1000; // 10秒;

for(var i = 0; i <= TheUrl.length-1; i ++){
setTimeout(function(i){
var url ='www.myurl (错误);返回回调(错误);
var $ = cheerio();
请求(url,函数(error,resp,body){
load(body);
//重新计算一次...
callback();
});
},interval * i,i);





$ b

所以第一个马上跑(间隔* 0是0),第二个一个在10秒后运行,等等。

您需要发送 i 作为<$ c中的最后一个参数$ c> setTimeout(),以便它的值绑定到函数参数。否则,尝试访问数组值将超出限制,您将得到 undefined


I have the code below:

var request = require('request');
var cheerio = require ("cheerio");
var async= require("async");

var MyLink="www.mylink.com";

    async.series([

        function(callback){
            request(Mylink, function (error, response, body) {
                if (error) return callback(error); 
                var $ = cheerio.load(body);
                //Some calculations where I get NewUrl variable...
                TheUrl=NewUrl;
                callback();
            });
        },
        function(callback){
            for (var i = 0; i <=TheUrl.length-1; i++) {
                var url = 'www.myurl.com='+TheUrl[i];
                request(url, function(error, resp, body) { 
                    if (error) return callback(error); 
                    var $ = cheerio.load(body);
                    //Some calculations again...
                    callback();
                });
            };
        }
      ], function(error){
        if (error) return next(error);
    });

Does anyone have a suggestion about how I can delay each loop iteration in the for loop? Say, the code waits 10 seconds after each iteration is complete. I tried setTimeout but didn't manage that to work.

解决方案

You can set a timeout for the execution of the code at increasing intervals like this:

var interval = 10 * 1000; // 10 seconds;

for (var i = 0; i <=TheUrl.length-1; i++) {
    setTimeout( function (i) {
        var url = 'www.myurl.com='+TheUrl[i];
        request(url, function(error, resp, body) { 
            if (error) return callback(error); 
            var $ = cheerio.load(body);
            //Some calculations again...
            callback();
        });
    }, interval * i, i);
}

So the first one runs right away (interval * 0 is 0), second one runs after ten seconds, etc.

You need to send i as the final parameter in the setTimeout() so that its value is bound to the function argument. Otherwise the attempt to access the array value will be out of bounds and you will get undefined.

这篇关于延迟节点js中的每个循环迭代,异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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