Sys.Preview.Timer Control - A Client Side Timer
We have quite often come across scenarios where-in we need to execute certain client-Side code at certain intervals .
What we often ended up doing is to use the window.setTimeOut() and the window.setInterval functions of IE to execute an expression
at certain intervals of time .
The Sys.Preview.Timer Provides an abstraction over these functions and makes it easier to define Functions to be Executed at certain
Time Intervals.
You need to include the Script file "PreviewScript.js" to have access to the Timer Control .
<script src="Scripts/PreviewScript.js" type="text/javascript"></script>
Instantiating the Timer control :
var clientSideTimer = new Sys.Preview.Timer();
By default , once instatiated , the Timer is disabled .
Enable The Timer
clientSideTimer.set_enabled( true );
Set the Interval for the timer :
The Interval is measured in milliseconds
clientSideTimer.set_interval( 1000 );
Add a function to be called at the interval
You add the function to be called at the interval by running
clientSideTimer.add_tick( OnTimerTick );
The Parameter "OnTimerTick" is a function that is executed at the Interval specified.
You can add multiple Functions to be callled at the interval.
The functions are executed at the interval in the same order that they are added using add_tick.
Stop the Timer
Disable the Timer to stop the Timer Entirely.
clientSideTimer.set_enabled( false );
Alternatively , you can stop certain functions being called by calling the "remove_tick " Method on the Timer.
clientSideTimer.remove_tick( OnTimerTick );
Check out the Sample attached to get a view of how the control works and how you can use it .
Whats Missing :
The main pain point I see in using the Timer Control is that I cannot pass a context to the Functions called at the "Tick" event.
The reason this happens is because ..
The Function that returns the callback to the "Tick" event passes "Sys.EventArgs.Empty" to the callback.
You can customize the Script to enable the functionality of passing a context to the Timer Callback.
We will discuss that in a future post .
Comments
Anonymous
March 14, 2007
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
October 30, 2007
Thanks for this item, but I am getting the error message clientSidetimer is null or not an object I even get the same message when I run the HTML example in the attachements. I have tried every possible combination, including placing the js script into the main page, but no success. Any ideas, please ??