最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - delay of 5 seconds between each request in Node Js - Stack Overflow

programmeradmin1浏览0评论

Here is my code:

I have more than 500,000 records in my database and I want to loop and request some information from another server. I've successfully wrote all functions except delays between each request. If I send all request with node.js remote server goes downs or can not answer each request. So I need to create a queue for looping But my code is not working still sending all request very fast.

var http = require('http')
, mysql = require('mysql');
var querystring = require('querystring');
var fs = require('fs');
var url  = require('url');


var client = mysql.createClient({
    user: 'root',
    password: ''
});

client.useDatabase('database');
client.query("SELECT * from datatable",
    function(err, results, fields) {
        if (err) throw err;
        for (var index in results) {
            username = results[index].username;
            setInterval(function() {

                requestinfo(username);
            }, 5000 );

        }
    }
    );
client.end();
}

Here is my code:

I have more than 500,000 records in my database and I want to loop and request some information from another server. I've successfully wrote all functions except delays between each request. If I send all request with node.js remote server goes downs or can not answer each request. So I need to create a queue for looping But my code is not working still sending all request very fast.

var http = require('http')
, mysql = require('mysql');
var querystring = require('querystring');
var fs = require('fs');
var url  = require('url');


var client = mysql.createClient({
    user: 'root',
    password: ''
});

client.useDatabase('database');
client.query("SELECT * from datatable",
    function(err, results, fields) {
        if (err) throw err;
        for (var index in results) {
            username = results[index].username;
            setInterval(function() {

                requestinfo(username);
            }, 5000 );

        }
    }
    );
client.end();
}
Share Improve this question edited Sep 20, 2012 at 8:51 iJade 23.9k58 gold badges160 silver badges250 bronze badges asked Sep 20, 2012 at 8:48 Kemal YKemal Y 591 gold badge2 silver badges6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Your problem lies in the for loop since you set all the requests to go off every 5 seconds. Meaning after 5 seconds all the requests will be fired relatively simultaneously. And since it is done with setInterval then it will happen every 5 seconds.

You can solve it in 2 ways.

First choice is to set an interval to create a new request every 5 seconds if the index is a number, so instead of a for loop you do something like:

    var index = 0;
    setInterval(function(){
      requestinfo(results[index].username);
      index++;
    }, 5000)

The second choice is to set all the requests with an increasing timeout so you modify your current script like so:

    var timeout = 0;
    for (var index in results) {
        username = results[index].username;
        setTimeout(function() {
            requestinfo(username);
        }, timeout );
        timeout += 5000;
    }

This will set timeouts for 0,5,10,15... etc seconds so every 5 seconds a new request is fired.

发布评论

评论列表(0)

  1. 暂无评论