main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | import { useState } from 'react';
|
---|
2 | import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
|
---|
3 | var useRaf = function (ms, delay) {
|
---|
4 | if (ms === void 0) { ms = 1e12; }
|
---|
5 | if (delay === void 0) { delay = 0; }
|
---|
6 | var _a = useState(0), elapsed = _a[0], set = _a[1];
|
---|
7 | useIsomorphicLayoutEffect(function () {
|
---|
8 | var raf;
|
---|
9 | var timerStop;
|
---|
10 | var start;
|
---|
11 | var onFrame = function () {
|
---|
12 | var time = Math.min(1, (Date.now() - start) / ms);
|
---|
13 | set(time);
|
---|
14 | loop();
|
---|
15 | };
|
---|
16 | var loop = function () {
|
---|
17 | raf = requestAnimationFrame(onFrame);
|
---|
18 | };
|
---|
19 | var onStart = function () {
|
---|
20 | timerStop = setTimeout(function () {
|
---|
21 | cancelAnimationFrame(raf);
|
---|
22 | set(1);
|
---|
23 | }, ms);
|
---|
24 | start = Date.now();
|
---|
25 | loop();
|
---|
26 | };
|
---|
27 | var timerDelay = setTimeout(onStart, delay);
|
---|
28 | return function () {
|
---|
29 | clearTimeout(timerStop);
|
---|
30 | clearTimeout(timerDelay);
|
---|
31 | cancelAnimationFrame(raf);
|
---|
32 | };
|
---|
33 | }, [ms, delay]);
|
---|
34 | return elapsed;
|
---|
35 | };
|
---|
36 | export default useRaf;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.