Skip to content

Commit 47cad16

Browse files
Create setIntervalPolyfill
1 parent 2a96b30 commit 47cad16

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function MyInterval(callback, delay) {
2+
let timerId;
3+
function repeat() {
4+
timerId = setTimeout(() => {
5+
callback();
6+
repeat(); // Call the function again
7+
}, delay);
8+
}
9+
10+
repeat();
11+
12+
return {
13+
clearInterval: () => clearTimeout(timerId),
14+
};
15+
}
16+
17+
// Usage Example:
18+
const interval = MyInterval(() => {
19+
console.log("Hello, world!");
20+
}, 1000);
21+
22+
// To clear the interval after 5 seconds
23+
setTimeout(() => {
24+
interval.clearInterval();
25+
console.log("Interval cleared");
26+
}, 5000);

0 commit comments

Comments
 (0)