Reputation: 79
I use Express framework, I have this function and I want to run it in intervals since server starts:
setTimeout(function () {
console.log('timeout completed');
}, 1000);
so i put this piece of code to the initial file of Express.js Framework called App.js but it only runs twice, where is the problem and how could I fix it?
Upvotes: 0
Views: 247
Reputation: 4159
The setTimeout
function runs only once when that amount of time is completed,
here in your case it is 1s(1000ms).
Instead you may want to use setInterval
which does the same thing except that it doesn't stop until you tell it so.
Here is an example :
var max = 3;
var i = 0;
var timer = setInterval(function(){
if(i === max){
// stop the timer from looping.
clearInterval(timer);
}else{
i++;
}
},1000);
Upvotes: 2
Reputation: 154
Looks like you are describing a cron sort of job, why don't you use any of the many node crons modules instead.
https://www.npmjs.com/package/node-cron
Check that out, pretty straight forward.
Upvotes: 1