Overview
Implement createInterval(callback, delay) that calls callback every delay ms and returns a cancel function.
Examples
let count = 0; const cancel = createInterval(() => count++, 100); setTimeout(cancel, 250); // count is 2
Solution
Reveal solution
function createInterval(callback, delay) {
const id = setInterval(callback, delay);
return function cancel() { clearInterval(id); };
}cancellable-interval.js
Cancellable Interval
easycodingJavaScriptTiming
Overview
Implement createInterval(callback, delay) that calls callback every delay ms and returns a cancel function.
Examples
let count = 0; const cancel = createInterval(() => count++, 100); setTimeout(cancel, 250); // count is 2
Solution
Reveal solution
function createInterval(callback, delay) {
const id = setInterval(callback, delay);
return function cancel() { clearInterval(id); };
}NameTopicDifficulty
103 of 103 problems