parsec-hooks 1.0.13 → 1.0.14
Sign up to get free protection for your applications and to get access to all the features.
@@ -19,9 +19,9 @@ var _default = function _default() {
|
|
19
19
|
countdown = _useRefState2[0],
|
20
20
|
setCountdown = _useRefState2[1];
|
21
21
|
|
22
|
-
var
|
22
|
+
var stopRef = (0, _react.useRef)(false);
|
23
23
|
var clearCountdownTimer = (0, _react.useCallback)(function () {
|
24
|
-
return
|
24
|
+
return stopRef.current = true;
|
25
25
|
}, []);
|
26
26
|
var stopCountdownTimer = (0, _react.useCallback)(function () {
|
27
27
|
clearCountdownTimer();
|
@@ -33,19 +33,40 @@ var _default = function _default() {
|
|
33
33
|
stopCountdownTimer: stopCountdownTimer,
|
34
34
|
setCountdown: (0, _react.useCallback)(function (s) {
|
35
35
|
return new Promise(function (resolve) {
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
36
|
+
stopRef.current = false;
|
37
|
+
setCountdown(s);
|
38
|
+
var startTime = +new Date();
|
39
|
+
|
40
|
+
var start = function start(prev) {
|
41
|
+
var now = +new Date();
|
42
|
+
|
43
|
+
if (stopRef.current) {
|
44
|
+
return;
|
45
|
+
}
|
46
|
+
|
47
|
+
if (now - prev > 1000) {
|
48
|
+
var count = s - Math.round((now - startTime) / 1000);
|
49
|
+
setCountdown(count);
|
50
|
+
|
51
|
+
if (count <= 0) {
|
52
|
+
resolve();
|
53
|
+
} else {
|
54
|
+
requestAnimationFrame(function () {
|
55
|
+
return start(now);
|
56
|
+
});
|
57
|
+
}
|
58
|
+
} else {
|
59
|
+
requestAnimationFrame(function () {
|
60
|
+
return start(prev);
|
61
|
+
});
|
45
62
|
}
|
46
|
-
}
|
63
|
+
};
|
64
|
+
|
65
|
+
requestAnimationFrame(function () {
|
66
|
+
return start(startTime);
|
67
|
+
});
|
47
68
|
});
|
48
|
-
}, [
|
69
|
+
}, [setCountdown]),
|
49
70
|
countdownText: countdown ? "(".concat(countdown, " s)") : ''
|
50
71
|
};
|
51
72
|
};
|