Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for Enhancing setInterval: Ability to Change Interval During Execution #57317

Closed
canawa opened this issue Mar 4, 2025 · 1 comment
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@canawa
Copy link

canawa commented Mar 4, 2025

What is the problem this feature will solve?

  1. Reducing Overhead with Timer Management
    Current Problem: When you need to change the interval of a repeating task, you currently have to clear the old interval and create a new one. This introduces unnecessary overhead, especially when intervals are frequent or need to be dynamically adjusted multiple times.
    With the Feature: Developers could adjust the interval without needing to clear and restart the timer, saving computational resources and simplifying the logic.
  2. Avoiding Redundant Operations
    Current Problem: Stopping and restarting intervals manually can lead to redundant operations. Each time you reset the interval, you must ensure that the previous one is cleared correctly. Mistakes here can cause bugs, like multiple timers running simultaneously.
    With the Feature: Changing the interval dynamically would reduce the need for redundant interval clear/reset logic, making code cleaner and less error-prone.
  3. Enabling Real-Time Adjustments
    Current Problem: In some use cases, the interval timing might need to adjust based on real-time data or user input. For example, if the time between operations needs to decrease after a certain event, or increase when load is high, developers need to stop and start intervals, adding complexity.
    With the Feature: Developers could adjust the interval "on-the-fly" without interrupting the flow of the application, improving responsiveness and flexibility in real-time scenarios.
  4. Improved UX for Interactive Applications
    Current Problem: In interactive applications where intervals control animations, updates, or polling, manually resetting intervals can disrupt the user experience. For example, changing the interval between polling requests or updates may require resetting the entire timer, causing undesirable side effects (like sudden pauses or delays).
    With the Feature: Dynamic interval adjustment would allow smoother transitions and more consistent behavior without noticeable disruptions, improving the overall user experience.
  5. Simplification of Code
    Current Problem: Developers often write complex logic to manage multiple timers and dynamically adjust their behavior. Managing intervals with multiple stop-and-start operations can lead to verbose and complex code.
    With the Feature: Being able to change the interval of a running timer would reduce the need for complex code and make it easier to manage multiple dynamic timers in an application.

What is the feature you are proposing to solve the problem?

Dynamic Interval Adjustment:
The ability to change the delay or interval between executions while the setInterval is running.

API Modification:
A new method, such as setInterval.setSpeed(newDelay), could be introduced, or an existing method could be extended, allowing developers to modify the interval during the timer's execution.

Simplified Timer Management:
Instead of manually stopping the interval with clearInterval and starting a new one, developers could adjust the interval directly, reducing unnecessary operations and potential errors in the code.

Real-Time Adjustments:
This feature would allow for real-time adjustments of the interval based on varying conditions (such as changing load, user input, or data), without interrupting or resetting the timer.

What alternatives have you considered?

  1. Manually Stopping and Restarting the Interval (Current Approach)
    How it Works: The common practice today is to use clearInterval() to stop the current interval and then create a new setInterval() with the new delay.
    Why it's not ideal: This approach involves unnecessary overhead because it requires stopping and restarting the interval, which can lead to timing inconsistencies, added complexity, and extra code for each change in interval. It's not the most efficient or intuitive solution.
  2. Using setTimeout Instead of setInterval
    How it Works: Instead of using setInterval, you can use setTimeout inside a function to create a recurring behavior, where you call the function again after it executes, allowing the delay to be dynamically adjusted each time.
    Why it's not ideal: While this works for some use cases, it introduces more complexity. You would need to manually re-trigger the timeout and manage the logic for recalculating the delay. It also doesn't truly mimic the behavior of setInterval, where the code is executed at fixed intervals without needing to manage the recursion manually.
  3. Using requestAnimationFrame for Animation-Related Tasks
    How it Works: For animation-related tasks, developers often use requestAnimationFrame to control the timing of animations. This method provides better control and smoother transitions for visual updates.
    Why it's not ideal: requestAnimationFrame is specifically designed for animation and isn't suitable for general-purpose repeated tasks like polling or event handling. It's also tied to the frame rate, which makes it less predictable for non-visual tasks.
  4. Custom Timer Classes or Libraries
    How it Works: Some developers build custom timer classes or use libraries that offer more advanced timer functionality, such as the ability to adjust the interval dynamically.
    Why it's not ideal: This approach introduces additional complexity and requires developers to either write custom code or depend on third-party libraries. It doesn't address the core issue within the native JavaScript API, making it a less standardized or widespread solution.
  5. Using Web Workers for Parallel Execution
    How it Works: In more advanced cases, developers may offload background tasks to Web Workers, running periodic tasks in a separate thread, which could provide more control over timing.
    Why it's not ideal: This is an over-engineered solution for most use cases. Web Workers are typically used for CPU-intensive tasks, not just adjusting intervals, and they involve more complexity than is necessary for managing simple timers.
    Why the Proposed Feature is the Best Solution:
    While the alternatives mentioned above each have their place in specific use cases, none of them provide the simplicity, efficiency, and native control that a feature allowing dynamic adjustment of the interval in a running setInterval would. By integrating this feature directly into the JavaScript API, developers would avoid the need for manual timer management, make their code cleaner and more efficient, and address the problem with minimal overhead and complexity.
@canawa canawa added the feature request Issues that request new features to be added to Node.js. label Mar 4, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Mar 4, 2025
@juanarbol
Copy link
Member

I appreciate the effort you put in this issue, but that is way beyond Node.js scope, the timers are part of the browser APIs, that's something we can't simply change.

See: https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers

Feel free to open a request in the right place, like the WHATWG or W3C -idk, ngl-.

@juanarbol juanarbol closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
Archived in project
Development

No branches or pull requests

2 participants