DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumJavaScript

Your Polling Service Needs a Controllable Repeating Timer With Start, Pause, and Stop

2 views

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:

  1. If the user switches tabs, we want to pause polling to save battery and network bandwidth.

  2. If the user logs out, we must stop and completely tear down the timer to prevent memory leaks.

  3. The API must be 'Bulletproof'—calling pause() before start() or calling start() 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.

Sample Test Cases

Case 1
Input
start, wait 350ms at 100ms interval
Expected Output
count===3
Case 2
Input
["function callback() { console.log('Polling...'); }", 100]
Expected Output
{"start": "function", "pause": "function", "stop": "function"}
Case 3
Input
["function callback() { return 'executed'; }", 100]
Expected Output
{"start": "function", "pause": "function", "stop": "function"}
Case 4
Input
["function callback() { this.count = (this.count || 0) + 1; }", 50]
Expected Output
{"start": "function", "pause": "function", "stop": "function"}
Case 5
Input
["function callback() { this.count = (this.count || 0) + 1; }", 50]
Expected Output
{"start": "function", "pause": "function", "stop": "function"}
Case 6
Input
pause then start resumes
Expected Output
resumes ticking
Case 7
Input
["function callback() { console.log('Polling...'); }", 1000]
Expected Output
{"start": "function", "pause": "function", "stop": "function"}
Case 8
Input
["function callback() { this.log.push('fired'); }", 10]
Expected Output
{"start": "function", "pause": "function", "stop": "function"}
Case 9
Input
pause before start
Expected Output
no error

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
2
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript