1.0.3 • Published 4 years ago
when-time v1.0.3
whenTime: A Simple Package To Do Things On Time In Node.js
Motivation
This package aims to simplify the task of scheduling tasks at specified time with a simple-to-use API. This can be helpful for making your applications restart at specific times in the day or having specific operations occur at a time period with repetitions.
Features
- Simple-to-use API
- Timezones Support
- Human Readable Parameters
- Chainable Syntax
- Memory Efficient
- Lightweight/No Dependencies
Installation
whenTime can be installed using node package manager (npm)
npm i when-timeExamples
Below are some examples making use of whenTime in various situations.
Scheduling multiple tasks at 3:30 AM to occur one-time
const whenTime = require('when-time');
// This is a one-time job which will execute the 2 tasks below once at 3:30 AM
whenTime.isEqualTo('3:30 AM')
.do(() => {
console.log('This is task 1');
}).do(() => {
console.log('This is task 2');
});Scheduling a task to begin at 3:30 AM and repeat every 12 hours
const whenTime = require('when-time');
whenTime.isEqualTo('3:30 AM')
.do(() => {
console.log('This task will repeat every 12 hours');
})
.repeat()
.every('12 hours');Scheduling a task to begin at 3:30 AM and repeat every 1 hour for a total of 12 times
const whenTime = require('when-time');
whenTime.isEqualTo('3:30 AM')
.do(() => {
console.log('This task will repeat every 1 hour for a total of 12 times after 3:30 AM');
})
.repeat(12)
.every('1 hour')
.whenFinished(() => {
console.log('Successfully trigger above task 12 times');
});Cancelling a scheduled task before execution
const whenTime = require('when-time');
let task = whenTime.isEqualTo('3:30 AM').do(() => {
console.log('Some task that will be cancelled');
});
// This will cancel the above task and clean up any underlying setTimeouts/setIntervals
task.cancel();TimeTask
Below is a breakdown of the TimeTask class generated when calling whenTime.isEqualTo().
Constructor Options
time_stringString: Time string representing a target time point.- Format:
Hours:Minutes:Seconds:Milliseconds AM/PM - Examples:
5:30 PM,10:30:55:850 PM,18:00,22:35:50:680 - Note 12 hour time format will only be used when
AM/PMis specified.
- Format:
TimeTask Properties
| Property | Type | Description |
|---|---|---|
state | String | Possible States: INITIALIZED, PENDING, STARTED, FINISHED, CANCELLED |
next_execution | Number | Timestamp of next execution in milliseconds |
tasks | Array | Returns all scheduled tasks with do(handler) |
remaining | Number | Number of repetitions remaining for task is finished |
TimeTask Methods
do(Function: operation): Adds an operation to the list of scheduled operations for aTimeTask.- Returns
TimeTasktherefore a chainable method. - Note operations will be executed in the order they were scheduled using this method on a
TimeTaskinstance.
- Returns
inTimezone(String: timezone): Specifies the timezone for thetime_stringspecified at creation of task.- See Timezones for supported timzones.
repeat(Number: amount): Sets the amount of times the scheduled tasks should repeat.- Default:
Infinity - Returns
TimeTasktherefore a chainable method. - Note:
whenFinished()is forbidden onceInfinityrepetitions have been set on an instance. - Note: Calling this method with
Infinityamount is forbidden after awhenFinished()handler has been set on an instance.
- Default:
every(String: interval): Specifies the interval after which repetitions should occur.- Returns
TimeTasktherefore a chainable method. - Format:
Amount MetricwhereMetric = [Day(s), Hour(s), Minute(s), Second(s), Millisecond(s)] - Note the default repetition interval for a
TimeTaskinstance is24 Hourswithout ever calling this method.
- Returns
whenFinished(Function: handler): Binds a handler which is triggered onceTimeTaskexecution is fully complete and all repetitions have been completed.- Returns
TimeTasktherefore a chainable method. - Note this method is forbidden when
Infinityamount of repetitions have been scheduled for an instance.
- Returns
cancel(): Cancels aTimeTaskand destroys all underlying timeouts/intervals for a complete cleanup.