Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F122102844
useRafInterval.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Jul 15, 19:16
Size
985 B
Mime Type
text/x-java
Expires
Thu, Jul 17, 19:16 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
27434864
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
useRafInterval.js
View Options
import { useEffect } from 'react';
import useCommittedRef from './useCommittedRef';
function useRafInterval(fn, ms, paused) {
if (paused === void 0) {
paused = false;
}
var handle;
var start = new Date().getTime();
var fnRef = useCommittedRef(fn); // this ref is necessary b/c useEffect will sometimes miss a paused toggle
// orphaning a setTimeout chain in the aether, so relying on it's refresh logic is not reliable.
var pausedRef = useCommittedRef(paused);
function loop() {
var current = new Date().getTime();
var delta = current - start;
if (pausedRef.current) return;
if (delta >= ms && fnRef.current) {
fnRef.current();
start = new Date().getTime();
}
cancelAnimationFrame(handle);
handle = requestAnimationFrame(loop);
}
useEffect(function () {
handle = requestAnimationFrame(loop);
return function () {
return cancelAnimationFrame(handle);
};
}, []);
}
export default useRafInterval;
Event Timeline
Log In to Comment