@class name="SvgTimer" hide="yes">
The timer class provides a convenient means for scheduling delayed or repeating function calls or method invocations
(c) SAP AG 2003-2006. All rights reserved.
<@cstor>
Creates a new timer
The new timer properties
function SvgTimer(props) {
for (var k in props) this[k] = props[k];
SvgTimer.register(this);
}
SVG.declareClass(SvgTimer);
//////////////////////////////////////////////////////////////////////////////////////
// PROPERTIES
<@prop name="target" type="Function|Object">Target function or object
<@prop name="onstart:s" default="">Timer start callback method name (in case target is an object)
<@prop name="onstop:s" default="">Timer stop callback method name (in case target is an object)
<@prop name="ontick:s" default="">Timer tick callback method name (in case target is an object)
<@prop name="delay:n:0">Delay period in ms (0 means no delay)
<@prop name="repeat:n:0">Repeat interval in ms (0 means no repeat)
<@prop name="maxcount:n:0">Max iterations count (0 means no limit)
<@prop name="maxtime:n:0">Max elapsed time (0 means no limit)
<@prop name="count:n:0" access="RO">Current iteration count
<@prop name="started:n:0" access="RO">Timer start timestamp in ms
<@prop name="stopped:n:0" access="RO">Timer stop timestamp in ms
//////////////////////////////////////////////////////////////////////////////////////
// METHODS
<@method name="start">
Starts the timer
SvgTimer.prototype.start = function() {
if (this.started || this.stopped) return;
this.count = 0;
this.started = (new Date()).getTime();
if (this.onstart) this.target[this.onstart](this);
this.timeout = window.setTimeout('SvgTimer.tick('+this.id+')', this.delay||0);
}
<@method name="stop">
Stops the timer
SvgTimer.prototype.stop = function() {
if (!this.started || this.stopped) return;
this.stopped = (new Date()).getTime();
if (this.timeout) window.clearTimeout(this.timeout);
this.timeout = null;
if (this.onstop) this.target[this.onstop](this);
SvgTimer.index[this.id] = null;
this.id = null;
}
<@method name="restart">
Restarts the timer
SvgTimer.prototype.restart = function() {
if (this.timeout) window.clearTimeout(this.timeout);
this.timeout = null;
this.started = 0;
this.stopped = 0;
if (!this.id) SvgTimer.register(this);
this.start();
}
//////////////////////////////////////////////////////////////////////////////////////
// PRIVATE
// index of all active timers
SvgTimer.index = [];
// advance a specified timer by one clock tick
SvgTimer.tick = function(id) {
try {
var t = SvgTimer.index[id];
if (!t || t.stopped) throw -1;
t.timeout = null;
t.count++;
if (t.maxcount && t.count >= t.maxcount) throw -1;
if (t.maxtime && t.started - (new Date()).getTime() >= t.maxtime) throw -1;
if (t.ontick) t.target[t.ontick](t); else t.target(t);
if (t.repeat) t.timeout = window.setTimeout('SvgTimer.tick('+t.id+')', t.repeat||0);
} catch(e) { t.stop(); }
}
// stop all running timers
SvgTimer.stopAll = function() {
for (var i=0, T=SvgTimer.index, len=T.length; i