We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2a96b30 commit 47cad16Copy full SHA for 47cad16
Interview Question/setIntervalPolyfill
@@ -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