question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

requestAnimationFrame is passed incorrect argument

See original GitHub issue
  • FakeTimers version : 10.0.0
  • Environment : Jest/JSDOM
  • Example URL : N/A
  • Other libraries you are using: N/A

What did you expect to happen?

The callback passed to requestAnimationFrame should receive an argument which is the number of seconds since time origin (same as performance.now()). MDN Docs

What actually happens

The callback instead receives clock.now which when compared to performance.now() is almost always orders of magnitude higher.

How to reproduce

// Sometime before:
clock.setSystemTime(new Date(2000, 10, 2));
// Test code
let lastFrameTime = performance.now();
window.requestAnimationFrame((time) => {
    console.log("Delta Time:", time - lastFrameTime); // This will log something like 973152000000
});

Suggestion

I know that performance.now() may not always be available. So I propose that this line be changed from:

args: [clock.now + getTimeToNextFrame()],

to

args: [(performancePresent ? clock.performance.now() : clock.now) + getTimeToNextFrame()],

(I am happy to create a PR if you’d like.)

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
CreativeTechGuycommented, Nov 24, 2022

Sorry, I used some loose language. To keep it simple, the current performance.now() should be passed to the callback when it is called. All of the handling of DOMHisResTimeStamp is already implemented in this library. It is currently used for hrtime() and performance.now(). So nothing fundamentally new needs to be added to make this work. Time origin is already being tracked and updated properly by the library. The only missing piece is that it isn’t being passed as the argument and instead a different time is.

0reactions
CreativeTechGuycommented, Dec 2, 2022

Okay! This PR is out!

(And for the record the other one is a pure addition which has no impact on anything that already exists so it isn’t breaking. But yeah let’s continue the discussion on that other thread.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

requestAnimationFrame passing an argument to the function it ...
The callback method is passed a single argument, a DOMHighResTimeStamp, which indicates the current time when callbacks queued by ...
Read more >
How To Add Arguments? A simple guide! - LiveFireDev
Basically, requestAnimationFrame takes as its first argument: a function that needs to be called. But the issue is that, we cannot pass any ......
Read more >
Window.requestAnimationFrame() - Web APIs | MDN
The callback method is passed a single argument, a DOMHighResTimeStamp , which indicates the current time (based on the number of milliseconds ...
Read more >
How can i pass arguments for function while using ... - Sololearn
I don't have much idea about js..but I have tried this :) function repeat() { // Do anything requestAnimationFrame(repeat); } requestAnimationFrame(repeat); ...
Read more >
Using requestAnimationFrame with a function that takes a ...
I'm playing around with request animation frame. ... If you want to pass in a function with arguments set to be passed in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found