Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

322
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda