Overview
Implement createTimeout(callback, delay) that schedules callback after delay ms and returns a cancel function.
Examples
let called = false;
const cancel = createTimeout(() => { called = true; }, 100);
cancel(); // callback never firesSolution
Reveal solution
function createTimeout(callback, delay) {
const id = setTimeout(callback, delay);
return function cancel() { clearTimeout(id); };
}cancellable-timeout.js
Cancellable Timeout
easycodingJavaScriptTiming
Overview
Implement createTimeout(callback, delay) that schedules callback after delay ms and returns a cancel function.
Examples
let called = false;
const cancel = createTimeout(() => { called = true; }, 100);
cancel(); // callback never firesSolution
Reveal solution
function createTimeout(callback, delay) {
const id = setTimeout(callback, delay);
return function cancel() { clearTimeout(id); };
}NameTopicDifficulty
103 of 103 problems