0

I've been trying to figure out how to use the timeout, problem with my code is that its way to fast, I could not discover how to do it with Request. everything else seems to work great

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();
3
  • Why do you want to call it every 3 seconds ? Commented Nov 12, 2014 at 10:49
  • Try replacing the for loop with a setInterval with a time of 3000 and increment the index manually Commented Nov 12, 2014 at 10:52
  • Nope, I think the best way for your application would be to use EventEmitter and raise an event when request is done. That way you'll be sure when another request is sent, the previous one is finished. Commented Nov 12, 2014 at 10:54

2 Answers 2

2

This is the gist of it using recursion:

    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
Sign up to request clarification or add additional context in comments.

3 Comments

For some reason I get that "i is not defined"
uhh yeah. I just typed it in a rush without testing. Variable i in userid[i] is undefined and should have been userid[index]. Updated answer.
Btw, the err-message gives you a line-number where the error occurred. Knowing how to interpret the errors goes a long way in solving the problem yourself. Good luck!
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.