The Core.Web.Scheduler
namespace provides capabilities for scheduling tasks to run after a certain amount of time, or simply in a different JavaScript execution context. It does this by providing an object-oriented wrapper around the Window
object's setTimeout()
method.
The Scheduler
namespace provides an abstract Runnable
class which is used to define a task to be executed. Runnable
implementations must provide a run()
method which will be invoked by the scheduler to perform the task when appropriate. Implementations may optionally define a timeInterval
property to specify the number of milliseconds after which the task should be executed, and a repeat
boolean flag which may be set to cause execution to repeated at that interval. If the timeInterval
property is not specified, the task will be executed immediately, but in a new JavaScript execution context.
A Runnable
can be scheduled for execution by passing it to the Core.Web.Scheduler.add()
method. Likewise, one can be canceled from future execution by passing it to the Core.Web.Scheduler.remove()
method.
The following code defines a Runnable implementation which raises an alert dialog after 5 seconds. An instance of the runnable is then created and scheduled:
DelayedMessageRunnable = Core.extend(Core.Web.Scheduler.Runnable, { _message: null, $construct: function(message) { this._message = message; }, timeInterval: 5000, run: function() { alert("Delayed message: " + this._message); } }); Core.Web.Scheduler.add(new DelayedMessageRunnable("Foo!"));
The namespace also provides several shorthands for scheduling code to be executed. The Core.Web.Scheduler.run()
method can be used to create a Runnable
and execute it immediately. The Core.Web.Scheduler.MethodRunnable
object provides a concrete implementation of Runnable
to simply execute a specific function or method.
The following example shows the use of Core.Web.Scheduler.run()
:
Core.Web.Scheduler.run(function() { alert("Foo!"); }, 5000, false);