Track setInterval IDs so one call cancels every interval
Imagine you’re working with JavaScript’s window.setInterval() function, which is commonly used to schedule tasks for future execution. Your challenge is to not only utilize setInterval effectively but also to create a function named clearAllIntervals(). This function should be capable of clearing all active interval timers, proving especially useful for cleaning up pending tasks before a page transition. The native clearInterval() only clears one interval at a time.
Consider the scenario where multiple functions are scheduled to execute after a delay, as illustrated in the example below:
setInterval(func1, 10000);
setInterval(func2, 10000);
setInterval(func3, 10000); // All three functions are scheduled for execution after a 10-second delay
The task is to implement the clearAllIntervals() function such that calling it cancels all previously scheduled tasks:
clearAllIntervals(); // Cancels all scheduled tasks
This challenge tests your ability to manipulate and work with timers in a frontend environment effectively. You are required to maintain the interface of window.setInterval and window.clearInterval but are allowed to augment their underlying logic to incorporate the functionality of clearAllIntervals().
This task not only assesses your proficiency with JavaScript timers but also your capacity to design clean and efficient solutions for managing asynchronous tasks in web applications.
Imagine you're working with JavaScript's window.setInterval() function, which is commonly used to schedule tasks for future execution. Your challenge is to not only utilize setInterval effectively but also to create a function named clearAllIntervals(). This function should be capable of clearing all active interval timers, proving especially useful for cleaning up pending tasks before a page transition.
Consider the scenario where multiple functions are scheduled to execute after a delay, as illustrated in the example below:
setInterval(func1, 10000); setInterval(func2, 10000); setInterval(func3, 10000); // All three functions are scheduled for execution after a 10-second delay The task is to implement the clearAllIntervals() function such that calling it cancels all previously scheduled tasks:
clearAllIntervals(); // Cancels all scheduled tasks This challenge tests your ability to manipulate and work with timers in a frontend environment effectively. You are required to maintain the interface of window.setInterval and window.clearInterval but are allowed to augment their underlying logic to incorporate the functionality of clearAllIntervals().
This task not only assesses your proficiency with JavaScript timers but also your capacity to design clean and efficient solutions for managing asynchronous tasks in web applications.