mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 17:15:48 +00:00
cancel previous animation frame when request new
This commit is contained in:
parent
e6bfd40472
commit
5b9bda680f
1 changed files with 7 additions and 8 deletions
|
|
@ -2,18 +2,17 @@ const React = require('react');
|
|||
|
||||
const useAnimationFrame = () => {
|
||||
const animationFrameId = React.useRef(null);
|
||||
const request = React.useCallback((cb) => {
|
||||
if (animationFrameId.current === null) {
|
||||
animationFrameId.current = requestAnimationFrame(() => {
|
||||
cb();
|
||||
animationFrameId.current = null;
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
const cancel = React.useCallback(() => {
|
||||
cancelAnimationFrame(animationFrameId.current);
|
||||
animationFrameId.current = null;
|
||||
}, []);
|
||||
const request = React.useCallback((cb) => {
|
||||
cancel();
|
||||
animationFrameId.current = requestAnimationFrame(() => {
|
||||
cb();
|
||||
animationFrameId.current = null;
|
||||
});
|
||||
}, []);
|
||||
return [request, cancel];
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue