Skip to content

Commit 10d0f14

Browse files
committedMay 23, 2022
feat: add setInterval implementation by setTimeout
1 parent 85032df commit 10d0f14

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎js/setTimeout-interval.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: Chacha
3+
* @Date: 2022-05-23 23:33:55
4+
* @Last Modified by: Chacha
5+
* @Last Modified time: 2022-05-23 23:34:16
6+
*/
7+
8+
const simulateSetInterval = (func, timeout) => {
9+
let timer = null;
10+
11+
const interval = () => {
12+
timer = setTimeout(() => {
13+
func();
14+
interval();
15+
}, timeout);
16+
};
17+
18+
interval();
19+
20+
return () => clearTimeout(timer);
21+
};
22+
23+
const cancel = simulateSetInterval(() => {
24+
console.log(1);
25+
}, 300);
26+
27+
setTimeout(() => {
28+
cancel();
29+
}, 1000);

0 commit comments

Comments
 (0)
Failed to load comments.