Components -> sabinos.Timer

Introduction

sabinos.Timer is a javascript timer class implementation based on Prototype framework.

Examples

This is the simplest sabinos.Timer example. It shows a function being called after the timer elapses.

Console:

HTML

<input id="buttonExample1" type="button" value="Start" onclick="example1();" />
<textarea id="textboxExample1"  rows="2" cols="70"></textarea>
    

JavaScript

//Function called after buton click
function example1() {
    //Creates sabinos.Timer object that calls 'example1Function' function 
    // '1' second after start (wich is the default interval).
    var timer = new sabinos.Timer({Elapsed:example1Log});
    //Start timer object
    timer.start();
    //Clear console textbox
    $('textboxExample1').value='';
}

//Function called after Example 1 timer elapsed
function example1Log(){
    $('textboxExample1').value='timer elapsed for Example 1\n';
}

The next example is a little more complex and show the usage of more functions of the sabinos.Timer class, such as stoping it at any time, getting how much time was elapsed after the start button is pressed, getting if the timer is running or not, setting it to reset automaticaly each time it is elapsed and limiting the number of times it will be automatically reseted.
This example also shows the usage of others events besides the 'Elapsed' showned at Example 1.

 

Console:

HTML

<input id="buttonExample2Start" type="button" value="Start" onclick="example2Start();" /> 
<input id="buttonExample2Stop" type="button" value="Stop" onclick="example2Stop();" />
<input type="checkbox" checked="checked" id="checkboxExample2" onclick="example2EnableChange(this)" />
<label for="checkboxExample2">Timer enabled</label>
<input id="buttonExample2Log" type="button" value="Get Timer Info" onclick="example2GetInfo();" />
    

JavaScript

    
//Creates sabinos.Timer object pointing to different functions for each event
var timer2 = new sabinos.Timer({
    AutoReset: true,                //Reset timer after each loop
    Interval: 2,                    //each loop takes 2 secunds to be fired after start/reset
    Loops: 5,                      //Reset will occur 10 times (set to -1 to repeat forever)
    Started: example2Started,       //'example2Started' function called each time timer2 starts
    Stoped: example2Stoped,         //'example2Stoped' function called each time timer2 stops
    Reseted: example2Reseted,        //'example2Reseted' function called each time timer2 resets
    Elapsed: example2Elapsed        //'example2Elapsed' function called each time timer2 elapses
    });

//Function called after button start is clicked
function example2Start() {
    //Start timer object
    timer2.start();
}
//Function called after button stop is clicked
function example2Stop(){
    if(timer2!=null)
        timer2.stop();
}

//function called after checkbox is checked or unchecked function example2EnableChange(checkboxElement){ if(timer2!=null) timer2.IsEnabled = $(checkboxElement).checked; else $(checkboxElement).checked = !$(checkboxElement).checked; } //function called each time timer2 elapses function example2Elapsed() { $('textboxExample2').value+='Example2 Timer Elapsed\n'; } //function called each time timer2 restes function example2Reseted() { $('textboxExample2').value+='Example2 Timer Reseted\n'; } //function called each time timer2 starts function example2Started() { $('textboxExample2').value='Example2 Timer Started\n'; } //function called each time timer2 stops function example2Stoped() { $('textboxExample2').value+='Example2 Timer Stoped\n'; } //Function called after button Update info is clicked function example2GetInfo(){ var timer2ElapsedSeconds = timer2.getElapsed(); //get amount of seconds elapsed since start action was performed var timer2LoopsCompleted = timer2.LoopsCompleted; //get amount of loops completed since start action was performed var timer2IsRunning = timer2.IsRunning; //get a bool value indicating if the timer is still runnning var timer2IsEnabled = timer2.IsEnabled; //get a bool value indicating if the timer is enabled var textBoxExample2 = $('textboxExample2'); textBoxExample2.value='Example 2 timer info.\n'; textBoxExample2.value+='getElapsed(): '+ timer2ElapsedSeconds + '.\n'; textBoxExample2.value+='LoopsCompleted: ' + timer2LoopsCompleted + '.\n'; textBoxExample2.value+='IsRunning: ' + timer2IsRunning + '.\n'; textBoxExample2.value+='IsEnabled: ' + timer2IsEnabled + '.\n'; }

Documentation

To use sabinos.Timer you must instanciate the timer class that will be able to call functions based on times intervals

Instance

Return Name Description
sabinos.Timer initialize([Hash options]) Creates an instance of sabinos.Timer

Options

Return Default Name Description
number 1 Interval Time, in seconds, before the Elapsed event is called relative to the last occurrence of the Elapsed event
boolean false AutoReset Indicates if the timer should reset after Elapsed event occur
number -1 Loops Amount of times that the timer resets if AutoReset is true. -1 value indicates that timer will repeat forever
function Prototype.emptyFunction Elapsed Function called when the specified timer interval has elapsed and if the timer is Enabled
function Prototype.emptyFunction Started Function called after the timer is started and if the timer is Enabled
function Prototype.emptyFunction Stoped Function called after the timer is stoped and if the timer is Enabled
function Prototype.emptyFunction Reseted Function called after the timer is reseted and if the timer is Enabled

Properties

Return Default Name Description
number 0 LoopsCompleted Amount of times the timer was automaticaly reseted
boolean false IsRunning Indicates if the timer is running or not
boolean true IsEnabled Indicates if the timer is enabled or not. If timer is not enabled and AutoReset is set to true the timer will continue to reset each time the time interval is elapsed but yet no event will be fired and Properties will remain the same. Otherwise if timer is not enabled and AutoReset is set to false no events will be fired after the timer interval is elapsed and timer will not be reset again.

Methods

Return Function Description
- start([bool resetLoopsCompleted, bool resetElapsedTime]) Starts the timer with optional arguments to reset the number of loops and reset the elapsed time. Defaul behaviour: start(true, true)
- stop() Stops the timer.
- reset() Rests the timer.
number getElapsed() Gets the number os seconds elapsed since the last time timer was started.

Download

You can find all important files for this and other controls at the Download section.