Write mySetInterval(fn, delay, ...args) and myClearInterval(id) without using setInterval.
mySetInterval calls fn(...args) every delay ms, the first time after delay ms, until it's cleared. It returns an ID. Several intervals can run at once, each with its own ID.
myClearInterval(id) stops the interval with that ID for good. It must also work when fn calls it to stop its own interval.
let count = 0;
const id = mySetInterval((label) => {
count++;
console.log(label, count);
if (count === 3) myClearInterval(id);
}, 1000, "tick");
// logs "tick 1", "tick 2", "tick 3", a second apart, then stops
const clock = useFakeTimers();
const fn = spy();
mySetInterval(fn, 100);
clock.tick(99);
expect(fn).toHaveBeenCalledTimes(0);
clock.tick(1);
expect(fn).toHaveBeenCalledTimes(1);
clock.tick(100);
expect(fn).toHaveBeenCalledTimes(2);
clock.tick(300);
expect(fn).toHaveBeenCalledTimes(5);
const clock = useFakeTimers();
const fn = spy();
mySetInterval(fn, 50, "a", 1);
clock.tick(100);
expect(fn).toHaveBeenCalledTimes(2);
expect(fn.calls[1].args).toEqual(["a", 1]);
const clock = useFakeTimers();
const fn = spy();
const id = mySetInterval(fn, 100);
clock.tick(300);
myClearInterval(id);
clock.tick(1000);
expect(fn).toHaveBeenCalledTimes(3);
const clock = useFakeTimers();
let id;
const fn = spy(() => {
if (fn.callCount === 2) myClearInterval(id);
});
id = mySetInterval(fn, 100);
clock.tick(1000);
expect(fn).toHaveBeenCalledTimes(2);
const clock = useFakeTimers();
const a = spy();
const b = spy();
const idA = mySetInterval(a, 100);
mySetInterval(b, 150);
clock.tick(300);
myClearInterval(idA);
clock.tick(300);
expect(a).toHaveBeenCalledTimes(3);
expect(b).toHaveBeenCalledTimes(4);
const clock = useFakeTimers();
const fakeSetInterval = globalThis.setInterval;
globalThis.setInterval = () => {
throw new Error("mySetInterval must not use setInterval");
};
const fn = spy();
try {
const id = mySetInterval(fn, 100);
clock.tick(250);
myClearInterval(id);
clock.tick(250);
} finally {
globalThis.setInterval = fakeSetInterval;
}
expect(fn).toHaveBeenCalledTimes(2);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with setInterval with setTimeout. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.