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

165
Views
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 answers
Answer question

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 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!