nodejs中的setTimeout

时间:2014-11-12 10:47:18

标签: javascript node.js

我一直在试图弄清楚如何使用超时,我的代码的问题是它的快速方式,我无法发现如何使用Request。其他一切看起来都很棒

var Scraper = function() {
    for (i in userid) {


        //create complete url 
        totalurl = facebookurl + userid[i];

        // request facebook open graph

// WARNING the request func should be called every 1-3 secs? 

       request(totalurl, function(error, response, html) {
            if (!error && response.statusCode == 200) {

                // console.log(html);




            }
        });

    };
};


    Scraper();

2 个答案:

答案 0 :(得分:2)

这是使用递归的要点:

    var userids = [//ids here];

    function processNextUrl(index){
        var userid = userids[index];
        totalurl = facebookurl + userid[index];
        request(totalurl, function(error, response, html) {
            if (!error && response.statusCode == 200) {
                //process
            }
            if(userids.length > index -1){ //stuff left to do
                setTimeout(function(){
                    processNextUrl(index+1);
                }, 3000);
            }
        });
    }

    processNextUrl(0); //kickstart the lot

答案 1 :(得分:0)

var EE = require('events').EventEmitter;

var Scraper = function(userId)
{
    this.Emitter = new EE();
    this.userID = userId;
    this.current = -1;
};
Scraper.prototype = 
{
    execute : function()
    {
        var _self = this;
        this.Emitter.on('_requestDone', function(data)
        {
            return _self.computeData(data);
        });
        this.Emitter.on('_errorHappened', function(e)
        {
            return console.log(e);
        });
        return this.sendRequest();
    },
    sendRequest : function()
    {
        this.current++;
        var totalurl = facebookurl + this.userID[this.current];

        var _self = this;
        request(totalurl, function(error, response, html)
        {
            if (!error && response.statusCode == 200)
            {
                return _self.Emitter.emit('_requestDone', html);
            }
            else
            {
                return _self.Emitter.emit('_errorHappened', error);
            }
        });
    },
    computeData : function(data)
    {
        // Perform your actions on data
        if(this.current < this.userID.length)
        {
            return this.sendRequest();
        }
        else
        {
            // When all the array has been computed
        }
    }
};

var S = new Scraper(/* Put your userId Array Here */);
S.execute();