FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Cancellable Interval

15mins

Implement a function cancellableInterval(callback, delay) that behaves like setInterval but returns a function to cancel the interval.

Example

let count = 0; const cancel = cancellableInterval(() => { count++; }, 100); setTimeout(() => { cancel(); // stop the interval }, 350); // After 350ms, count should be 3 (roughly)

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests