FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Use Interval

20mins

Implement a hook useInterval(callback, delay) that repeatedly calls a given function at a specified interval.

The hook should:

  • Accept a callback function and a delay in milliseconds.
  • Return a cleanup function to stop the interval.
  • Automatically handle updating the interval if the callback changes.
  • Properly clear the interval when no longer needed.

Example

let count = 0; const stop = useInterval(() => { count += 1; }, 1000); setTimeout(() => { stop(); // stops the interval }, 5000);

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests