Reputation: 139
So I want to make a program that would get data from a web api every 15 minutes and process it. That process would be endless (untill the program is closed). I've experimented with timeouts but my PC quickly runs out of memory (24GB of RAM), so my code is obviously a very bad way to do it.The processing and gathering data part of code is done and works fine, what isn't working is the endless loop part. My take on this problem was essentially this:
for (i = 0;; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
//More code goes in here.
console.log(i);
}, 1000);
}
Surely there must be a better way to execute a chunk of code every X amount of minutes endlessly without flooding you PC's RAM. Any ideas?
Upvotes: 1
Views: 1099
Reputation: 150772
You might want to use setInterval
, which does exactly what you need. The snippet
setInterval(function () {
console.log('Some message...');
}, 100);
will run the console.log
statement every 100 milliseconds, without messing with the stack or wasting system resources.
Upvotes: 1