delay of 5 seconds between each request in Node Js
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.
Kemal Y
I love web development and coding more than 15 years. My focus: PHP, JavaScript and Java My favorites: ReactJS, Laravel, Slim Framework and Symfony
Updated on June 05, 2022Comments
-
Kemal Y almost 2 years
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(); }
-
Kemal Y over 11 yearsI wish to send one by one request
-
DeadAlready over 11 yearsYour question code is lacking information on the query itself. If it is asynchronous as node requests usually are then you should ditch the defined delay and in the requestInfo callback initiate a new request. This will create a new request as soon as the old one is answered.