Custom Interval - Problem
Custom Interval System
You need to implement a custom interval system that schedules function execution with dynamically increasing delays. Unlike the standard
Where
Implement two functions:
1.
2.
Example Timeline:
• First execution: after
• Second execution: after
• Third execution: after
This creates a linear progression of delays, useful for implementing backoff strategies, progressive polling, or adaptive scheduling systems.
You need to implement a custom interval system that schedules function execution with dynamically increasing delays. Unlike the standard
setInterval which uses fixed delays, your custom interval should calculate delays using the formula:next_delay = delay + period × countWhere
count starts at 0 and increments after each execution.Implement two functions:
1.
customInterval(fn, delay, period) - Returns an ID and schedules fn to execute with increasing delays2.
customClearInterval(id) - Stops the interval identified by idExample Timeline:
• First execution: after
delay + period × 0 = delay ms• Second execution: after
delay + period × 1 = delay + period ms• Third execution: after
delay + period × 2 = delay + 2×period msThis creates a linear progression of delays, useful for implementing backoff strategies, progressive polling, or adaptive scheduling systems.
Input & Output
basic_interval.js — JavaScript
$
Input:
fn = () => console.log('executed'), delay = 100, period = 50
›
Output:
Executions at: 100ms, 250ms, 450ms, 700ms, ...
💡 Note:
First execution after 100ms, then each subsequent execution has 50ms more delay (100+50×1=150ms, 100+50×2=200ms, etc.)
clear_interval.js — JavaScript
$
Input:
id = customInterval(() => console.log('test'), 200, 100); customClearInterval(id);
›
Output:
No executions (cleared before first execution)
💡 Note:
The interval is cleared before the first execution at 200ms, so nothing gets printed
multiple_intervals.js — JavaScript
$
Input:
Two intervals: (fn1, 100, 0) and (fn2, 50, 100)
›
Output:
fn1 executions: 100ms, 200ms, 300ms... | fn2 executions: 50ms, 250ms, 550ms...
💡 Note:
First interval has constant 100ms delay (period=0), second has increasing delays: 50, 150, 250, 350ms...
Constraints
- 1 ≤ delay ≤ 104 (delay in milliseconds)
- 0 ≤ period ≤ 103 (period increment in milliseconds)
-
Function
fnwill not throw exceptions - Maximum 100 concurrent intervals
- IDs must be unique and persistent until cleared
Visualization
Tap to expand
Understanding the Visualization
1
Initial Setup
Create interval with delay=100ms, period=50ms
2
First Execution
Execute after 100ms (100 + 50×0)
3
Second Execution
Execute after 150ms (100 + 50×1)
4
Third Execution
Execute after 200ms (100 + 50×2)
5
Pattern Emerges
Each execution has progressively longer delay
Key Takeaway
🎯 Key Insight: Instead of polling continuously, let each interval calculate and schedule its own next execution using recursive setTimeout calls. This creates precise timing with minimal system overhead.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code