JsTimer - Qcubed JavaScript Timer
In the following example JsTimer is used to update a datagrid displaying the
incoming orders of a fictional web shop.
JsTimer can be used as a periodic or a one-shot timer.
This example shows the periodic one.
Take a look at the example below. The update timer $ctlTimer
is created with $this->ctlTimer = new JsTimer($this,3000,true,true);
The second parameter sets the update interval (in ms),the third parameter defines
a periodic timer and the fourth parameter tells the timer to start automatically after the first action is added.
If you want the timer to stop after it has fired, set the third parameter to false or call ->Periodic = false
on your timer instance. You can restart the timer by simply calling ->start($intNewTime). Without parameter
the old delay/interval is used.
You can add actions to JsTimer like you would do for any other control.
There is one limitation:
JsTimer accepts only one type of Event: TimerExpired,
adding multiple actions to one Event is possible.
Look at: $this->ctlTimer->addAction(new TimerExpired(), new Ajax('onUpdateDtg'));
When the defined time has passed by, the onUpdateDtg() method is called. In this method you could
fetch new orders from a database or from a web-service ...
for this example, adding an order to an array and marking the datagrid $dtgOrders
as modified, should be sufficient.
If you look at the template file you will notice that there is no $ctlTimer->render().
That is where JsTimer differs from other controls. There is no need to render it!
If a server action is executed, the whole page gets reloaded. So all JavaScript is stopped and
the timer is stopped too! In the case of our web shop we want to get informed about new orders after a server
action. To address this problem you can set the parameter RestartOnServerAction
of JsTimer to true.