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:
- Created 10 months ago
- Comments:8 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 ofDOMHisResTimeStamp
is already implemented in this library. It is currently used forhrtime()
andperformance.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.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.)