Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
Why are setTimeout and setInterval working differently in the browser and in Node.js?

let cnt=0;

let i = setInterval(() => {
  console.log(cnt++);
},200)

setTimeout(() => {
  clearInterval(i);
},2000);

When executed in browser this code logs:- 0 1 2 3 4 5 6 7 8 9

But when executed using Node it logs:- 0 1 2 3 4 5 6 7 8

What is the reason for this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is because Chrome does self-correct the drift in setInterval. node (and other browsers) don't. Note that currently specs agree with node, even if there is an active discussion to follow Chrome's behavior.

So in Chrome, it's like if you had a precise

at beginTime + 200
  do fn
at beginTime + 400
  do fn
at beginTime + 600
  do fn
...etc

while in others it will be

at beginTime + 200
  do fn
  at now + 200
    do fn
    ...etc.

But we know there will always be some delay preventing the timer to fire exactly when scheduled, and so in environments without drift correction, we end up with a now that is later the expected time.

So in Chrome, the setInterval job will be in the queue before the setTimeout one, while in other environments, it will come after, because of the drift, even if infinitesimally small.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!