We are building a Distributed Health Monitoring Dashboard. Our frontend needs to poll various microservice endpoints every 30 seconds to update status lights.
The requirements are specific:
If the user switches tabs, we want to pause polling to save battery and network bandwidth.
If the user logs out, we must stop and completely tear down the timer to prevent memory leaks.
The API must be 'Bulletproof'—calling
pause()beforestart()or callingstart()multiple times should not create multiple overlapping intervals (a common bug known as 'Timer Drifting').
The Challenge: Implement a createInterval(callback, interval) utility that returns an object with start, pause, and stop controls. Your solution must ensure that only one interval instance exists at any time and that the state transitions are idempotent.