Hone logo
Hone
Problems

Simple Task Scheduler in JavaScript

This challenge asks you to build a basic task scheduler in JavaScript. A task scheduler allows you to schedule functions to be executed at specific times or intervals. This is a fundamental component in many applications, from background processing to automated tasks.

Problem Description

You are to implement a Scheduler class that allows users to schedule tasks to be executed either once at a specific time or repeatedly at a given interval. The scheduler should manage these tasks and execute them according to their schedules.

Key Requirements:

  • addOnce(task, time): Schedules a task (a function) to be executed once at the specified time (in milliseconds from the epoch).
  • addInterval(task, interval): Schedules a task (a function) to be executed repeatedly every interval milliseconds.
  • remove(task): Removes a scheduled task from the scheduler. This should stop any future executions of the task, whether it was scheduled with addOnce or addInterval.
  • start(): Starts the scheduler, initiating the execution of scheduled tasks.
  • stop(): Stops the scheduler, preventing any further execution of scheduled tasks.

Expected Behavior:

  • Tasks scheduled with addOnce should execute exactly once at the specified time.
  • Tasks scheduled with addInterval should execute repeatedly at the specified interval, starting after the initial delay.
  • The scheduler should handle multiple tasks concurrently.
  • The scheduler should gracefully handle tasks that throw errors during execution (the error should not crash the scheduler, and subsequent tasks should still execute).
  • remove(task) should reliably stop a task, even if it's currently executing or scheduled for future execution.

Edge Cases to Consider:

  • Invalid time/interval values (e.g., negative values). The scheduler should handle these gracefully (e.g., by ignoring the task or throwing an error – specify your choice in the Notes section).
  • Tasks that take a long time to execute.
  • Tasks that are removed while they are executing.
  • Multiple calls to remove(task) for the same task.
  • Scheduling tasks very close to the current time.

Examples

Example 1:

Input:
scheduler = new Scheduler();
task1 = () => console.log("Task 1 executed");
task2 = () => console.log("Task 2 executed");

scheduler.addOnce(task1, Date.now() + 2000); // Execute task1 in 2 seconds
scheduler.addInterval(task2, 1000); // Execute task2 every 1 second
scheduler.start();

// After 3 seconds:
// Output:
// Task 2 executed
// Task 2 executed
// Task 2 executed
// Task 1 executed

Explanation: task1 executes once after 2 seconds, and task2 executes every second starting immediately after the scheduler starts.

Example 2:

Input:
scheduler = new Scheduler();
task = () => console.log("Task executed");
scheduler.addInterval(task, 500);
scheduler.remove(task);
scheduler.start();

// After 1 second:
// Output: (Nothing)

Explanation: The task is removed before it has a chance to execute.

Example 3: (Edge Case)

Input:
scheduler = new Scheduler();
task = () => { throw new Error("Task failed!"); };
scheduler.addOnce(task, Date.now() + 1000);
scheduler.start();

// After 2 seconds:
// Output: (Scheduler continues running, no crash)

Explanation: The scheduler should not crash if a task throws an error. Subsequent tasks should still be executed.

Constraints

  • time and interval values are expected to be numbers representing milliseconds.
  • The scheduler should be able to handle at least 100 concurrent tasks without significant performance degradation.
  • The task function should be a standard JavaScript function.
  • The scheduler should use setTimeout or setInterval for scheduling.
  • The scheduler should not rely on external libraries.

Notes

  • Consider how you will store the scheduled tasks and their associated timers.
  • Think about how to handle errors that occur within the scheduled tasks. Should the scheduler stop, or should it continue executing other tasks? Specify your choice in your code comments.
  • You can choose to throw an error if time or interval are invalid (e.g., negative). Document your choice.
  • Focus on clarity and maintainability of your code. Good commenting is appreciated.
  • The remove function needs to be robust to ensure tasks are stopped even if they are currently executing. Consider how to achieve this.
Loading editor...
javascript