user1062524
user1062524

Reputation: 63

Node.js synchronous HTTP client requests at timed intervals

I am a newcomer to node.js and what I would like to do is write a bot which fetches market prices every 0.5s and store them in an array. I.e. call something like the following but for it all to happen sequentially every 0.5s:

var events_req = https.request(options, function (res) {
    var body = '';
    res.setEncoding('utf-8');
    res.on('data', function (chunk) {
        body += chunk;
    });
    res.on('end', function () {
        if(p.parse(body)) {
            for (var i in MarketPrices) {
                eyes.inspect(MarketPrices[i]);
            }
        }
    });
});
events_req.write(post_data);
events_req.end();

How would I do this?

P.S.: Don't worry about the eyes.inspect - that's only there for debugging purposes so I can see what's happening.

Upvotes: 1

Views: 1431

Answers (1)

Teemu Ikonen
Teemu Ikonen

Reputation: 11929

Define function that does what you want and use setInterval

function fetch() { .. /* make request here */ }

setInterval( fetch, 500 );

Upvotes: 2

Related Questions