0

Im doing this:

setInterval(function(){
    // some stuff that takes not even 1ms
    console.log(Date.now())
}, 50);

turns out it runs every ~70 ms or so. This is in node and nothing heavy is running on my machine at all. I am aware of setInterval / setTimeout not always running perfectly, but running at ~70 ms when i put 50 is something ive never seen.

tennesea
  • 9
  • 2
  • I think this has already been answered in a previous question : https://stackoverflow.com/questions/23981382/setinterval-delays-not-accurate. See the answer for more details. – joprocorp Nov 06 '21 at 18:06
  • Does this answer your question? [setInterval delays not accurate](https://stackoverflow.com/questions/23981382/setinterval-delays-not-accurate) – esqew Nov 06 '21 at 18:38

1 Answers1

0

Either the machine is slow, or node.js is running too many processes at once.
If not, you can use a for loop:

setInterval(function() {
  for (let i = 0; i < 10; i++) {
    // Do what you want here.
  }
}, 500);

Please note that if you make it too fast, your machine will have problems.

Parking Master
  • 551
  • 1
  • 4
  • 20