Timer Plugin for JavaScript
-
Download files !
-
Add the code inside the body tag.
<script src="mx-timer.js"></script>
-
Start coding freely :)
-
Easily create timers.
var myTimer = new Timer({ func: function () { console.log('is timer function'); }, interval: 1000, // interval 1 second. autoStart: true // Automatic start timer. });
-
Define events within options.
var myTimer = new Timer({ func: function () { console.log('is timer function'); }, interval: 2000, // interval 2 second. autoStart: true, // Automatic start timer. initializer: function () { console.log('On initialize timer.'); // This function calls the timer constructor. }, onafterstart: function () { console.log('After start timer.'); // This function is executed every time after the timer runs. }, onstart: function () { console.log('Start timer.'); // This function is executed every time the timer runs. }, onafterstop: function () { console.log('After stop timer.'); // This function is executed after it has been stopped in time. }, onstop: function () { console.log('Stop timer.'); // This function is executed when the timer is stopped. }, callback: function () { console.log('Callback timer.'); // Callback function } });
-
Define events after create timer.
var myTimer = new Timer({ func: function () { console.log('is timer function'); }, interval: 2000, // interval 2 second. autoStart: true, // Automatic start timer. }); myTimer.on('start', function ($this) { console.log('Starting'); }).on('stop', function () { console.log('stopping'); });
Option Name | Type | Details |
---|---|---|
func | function | Function to be called |
interval | int | Interval in miliseconds. (1s = 1000 ms) |
group_name | string | If you want to group the timer, enter the group name. Note: The _global group name is used by the plugin. |
autoStart | boolean | Auto start timer. |
initializer | function | This function calls the timer constructor. |
onafterstart | function|function[] | This function is executed every time after the timer runs. |
onstart | function|function[] | This function is executed every time the timer runs. |
onafterstop | function|function[] | This function is executed after it has been stopped in time. |
onstop | function|function[] | This function is executed when the timer is stopped. |
callback | function|function[] | Callback function |
Method Name | Return Type | Details |
---|---|---|
Timer.getCount | int | The number of times the timer works |
Timer.isStarted | boolean | Timer is started ? |
Timer.trigger | Timer | Trigger timer events. |
Timer.on | Timer | Binding events. |
Timer.start | Timer | Start timer. |
Timer.reset | Timer | Reset timer. Like Timer.stop(true).start(); |
Timer.stop | Timer | Stop timer. |
MXTimer.add({/* Options */}) | Timer | Adding timer. |
MXTimer.remove(_/** Optional */_'group_name') | MXTimer | Remove timer by group name. Note: If no group name is sent, removes all timers. |
MXTimer.stop(_/** Optional */_'group_name') | Timer | Stop timer by group name. Note: If no group name is sent, stopped all timers. |
MXTimer.getByGroupName('group_name') | Timer | Get timers by group name. |
MXTimer.info(_/** Optional */_'group_name') | Timer | Timer information by group name. Note: If the group name is not sent, it returns information for all timers. |
Event Name | Details |
---|---|
onafterstart | This function is executed every time after the timer runs. |
onstart | This function is executed every time the timer runs. |
onafterstop | This function is executed after it has been stopped in time. |
onstop | This function is executed when the timer is stopped. |
Error | Class | Details |
---|---|---|
Invalid argument was sent for adding timer. | TimerInvalidAddArgs | If a value other than object type is entered while adding the timer, you will get this error message. You can get help from the examples above when adding timers. |
Timer function not defined correctly. | TimerIsNotFunction | The timer has to be defined as a function. The reason for encountering this error is to define a real function. |
Invalid Interval value. Interval to be bigger than zero. | TimerInvalidAddArgs | Timer interval must be greater than zero. |