taskschedulerbyandretanko
v1.0.1
Published
by Andretanko
Downloads
1
Readme
Task Scheduler
Task Scheduler is a simple JavaScript package that allows you to schedule tasks to run at specific intervals in your Node.js applications.
Installation
You can install Task Scheduler via npm:
npm install task-scheduler
Usage
const TaskScheduler = require('task-scheduler');
// Create a new instance of TaskScheduler
const scheduler = new TaskScheduler();
// Add a new task to the scheduler
const taskId = scheduler.addTask(() => console.log('Task is running...'), 2000);
// Remove a task from the scheduler
scheduler.removeTask(taskId);
// List all scheduled tasks
console.log('Scheduled tasks:', scheduler.listTasks());
API
new TaskScheduler()
Creates a new instance of the TaskScheduler class.
addTask(taskFn, intervalMs)
Adds a new task to the scheduler.
taskFn
: A function representing the task to be executed.intervalMs
: The interval in milliseconds at which the task should be run.
Returns the ID of the added task.
removeTask(taskId)
Removes a task from the scheduler.
taskId
: The ID of the task to be removed.
Returns true
if the task was successfully removed, false
otherwise.
listTasks()
Lists all scheduled tasks along with their IDs and next run times.
Returns an array of objects representing the scheduled tasks.
Example
const TaskScheduler = require('task-scheduler');
// Create a new instance of TaskScheduler
const scheduler = new TaskScheduler();
// Add a new task to the scheduler
const taskId = scheduler.addTask(() => console.log('Task is running...'), 2000);
// List all scheduled tasks
console.log('Scheduled tasks:', scheduler.listTasks());
// Remove the added task after 10 seconds
setTimeout(() => {
scheduler.removeTask(taskId);
console.log(`Task with ID ${taskId} removed.`);
console.log('Scheduled tasks after removal:', scheduler.listTasks());
}, 10000);
License
This project is licensed under the MIT License - see the LICENSE file for details.