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

156
Vistas
How does the variable times works out in this specific line of code

I currently trying to learn how to animation in javascript works and I got a little bit stuck. The blog that I am reading is https://javascript.info/js-animation and this pice of code just drives me crazy.

let prev = performance.now();
let times = 0;

requestAnimationFrame(function measure(time) {
  document.body.insertAdjacentHTML(
    "beforeEnd",
    Math.floor(time - prev) + " "
  );
  
  prev = time;

  if (times++ < 10) requestAnimationFrame(measure);
})

I cannot uderstand what the variable times is all about, why he use time in the Math.floor method and why prev = time. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

times

Here, times is used as a counter to call the requestAnimationFrame function 10 times:

What this line does:

if (times++ < 10) requestAnimationFrame(measure);
  • Check if times is less than 10
  • If so, call requestAnimationFrame
  • Increment times

time = prev

time holds the timestamp at the function call, with very high accuracy. This is then assigned to prev, which is presumably used to hold the timestamp during the previous function call

The reason for this is explained in the next section


Math.floor(time - prev)

Here, time holds the time during the current function call with very high precision, and prev the time during the previous function call.

If you subtract prev from time, You'll get the amount of time elapsed between the two.

The Math.floor function then rounds this down to an integer, after which it is inserted into the body using the document.body.insertAdjacentHTML function.

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