timr offers a scheduling interface for a cron like job execution.
installation
npm install timr
example
var moment = ; var timr = ; var scheduler = ; ////every 15 seconds; //start 30 seconds from now, execute every 10 seconds, end in 5 minutes from nowvar from = ;var to = ; ;
scheduler
a scheduler holds a collection of tasks. every task is created via the task construction function.
creates a task construction function
var timr = ; var taskConstructor = ;
the scheduler object is exposed at the task constructor:
var timr = ; var taskConstructor = ; console;
when a attached task is executed, the parent scheduler also emits a execution event
var timr = ; var taskConstructor = ; //...create some tasks... taskConstructorscheduler;
task
modifiers
specify how often and when a task should be performed.
when
via the methods .from(timestamp)
and .to(timestamp)
the period of time can be specified in which the task should be performed.
these methods can be used in every combination or can be omitted completely. (which would case a task to run instantly and indefinitely.
how often
the quantifier .every(n)
in combination with a interval modifier like .hour()
, .minute()
etc defines how often a task gets executed.
the n
parameter can be omitted, it defaults to 1
. (task.every().minute()
means that a task is executed once a minute)
for example the expression task.every(2).minutes()
executes the task every 2 minutes.
there are five interval modifiers:
.second()
.minute()
.hour()
.day()
.month()
for every modifier also the plural form is valid. (e.g. .minutes()
instead of .minute()
)
creation
tasks geht automatically attached to the parent scheduler object
var myTask = ;
there are multiple ways of invoking a task.
anonymous (one callback)
creates the task, configures it to run every minute and runs the callback assigned in the run handler
;
anonymous (multiple callbacks)
creates the task, configures it to run every minute and runs each callback.
;
named (event handler attached to the task)
creates the task, configures it to run every minute and runs the callback assigned in the run handler.
run
has to be called.
var myTask = ; myTask; myTask; myTask;
event handler attached to the scheduler
creates two anonymous tasks, configures to run the first every minute and the second to run every second hour.
than the events emitted on the scheduler are used to consume the task events.
run
has to be called.
;; taskConstructorscheduler;
tests
npm test