1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.