Schedule decorators for TypeScript
The schedule decorators library provides a simple, ES5+ compatible, lightweight and universal decorator to easily make recurrent function calls. The decorator uses the standard window.setInterval() function internally, makes the code clearer and avoid code duplication.
Install
The easiest way is to install ts-schedule-decorators
as dependency
:
npm install ts-schedule-decorators --save
Usage
Schedule an instance method
Instance methods will be executed after instantiation.
// Displays:// 2017-01-09 12:12:35 Meeeooow// 2017-01-09 12:12:36 Meeeooow// 2017-01-09 12:12:37 Meeeooow...
Schedule a static method
Static methods will be executed on declaration.
// Displays:// 2017-01-09 12:16:41 Miaaooow// 2017-01-09 12:16:42 Miaaooow// 2017-01-09 12:16:43 Miaaooow...
Allow manual invocation
By default and to be safe, an interval method cannot be invoked manually. However, you can avoid this behavior by setting the protectOriginal
option to false
.
Cat.miaow// Displays:// Uncaught Error: interval method cannot be invoked Cat.miaow// Displays:// 2017-01-09 12:55:33 Miaaooow// 2017-01-09 12:55:34 Miaaooow// 2017-01-09 12:55:35 Miaaooow// 2017-01-09 12:55:36 Miaaooow
Execute the method on the leading edge
If the option leading
is set to true
, then the method will be invoked on the leading edge as well.
// Displays:// 2017-01-09 12:18:18 Miaaooow// 2017-01-09 12:18:19 Miaaooow// 2017-01-09 12:18:20 Miaaooow// 2017-01-09 12:18:21 Miaaooow...
Stopping condition
The execution of the method can be stopped providing a function to the stop
option. If the invokation of this function returns true
, then the interval is cleared.
// Displays:// 2017-01-09 12:18:18 Miaaooow// 2017-01-09 12:18:19 Miaaooow// 2017-01-09 12:18:20 Miaaooow// 2017-01-09 12:18:21 Miaaooow...
License
Code licensed under MIT License.