Implement a utility useTimeout(callback, delay) that invokes a callback function after a specified delay.
Unlike using plain setTimeout, this utility should be reactive to changes in callback or delay values:
delay is null, the timeout should be cleared and the callback never runs.Your task: Implement useTimeout(callback, delay) so that:
Example
let loading = true; useTimeout(() => { loading = false; }, 1000); // Initially: loading = true // After 1000ms → loading = false
This mimics React’s useTimeout pattern but can be used in plain JavaScript too.
JavaScript Function
No test results yet
Click "Run" to execute tests