1

I am very sorry to bother you with a question I suspect to be trivial for anyone but myself. I tried my best to find an answer on my own, but was unable to.

For you to understand what I lack understanding of, I will provide you with an example taken from an answer to another question asked here.

My question in the context of this example: What is the behavior of newtime? Why is it required to be defined as a paremeter for this function to work? Where does it get its value?

startAnimating(5);

function startAnimating(fps) {
    fpsInterval = 1000 / fps;
    then = window.performance.now();
    startTime = then;
    console.log(startTime);
    animate();
}

function animate(newtime) {
    // (...)
    requestAnimationFrame(animate);
    now = newtime;
    elapsed = now - then;

    if (elapsed > fpsInterval) {
        then = now - (elapsed % fpsInterval);
        // (...)
    }
}

I will greatly appreciate any response. Thank you.

  • The code you used does not match the code in the question you linked. That one used `Date.now()` instead of newtime – Luca Kiebel Aug 22 '18 at 15:13
  • 1
    Check the documentation for [`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) and its callback – Andreas Aug 22 '18 at 15:14
  • @Luca I linked the exact answer I referred to. Thank you for your help everybody. –  Aug 22 '18 at 15:29

1 Answers1

1

Where does it get its value

requestAnimationFrame will call your function in the future with that passed in, and you need that value to compute the elapsed time.

callback

A parameter specifying a function to call when it's time to update your animation for the next repaint. The callback has one single argument, a DOMHighResTimeStamp, which indicates the current time (the time returned from performance.now() ) for when requestAnimationFrame() starts to fire callbacks.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame#Parameters

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247