rhymix/modules/editor/skins/xquared/javascripts/Timer.js
haneul 7c3b336e41 17223554 : xquared upgrade to 0.7
git-svn-id: http://xe-core.googlecode.com/svn/sandbox@4968 201d5d3c-b55e-5fd7-737f-ddc643e51545
2008-11-24 08:52:19 +00:00

90 lines
No EOL
1.6 KiB
JavaScript

/**
* @requires Xquared.js
*/
xq.Timer = xq.Class(/** @lends xq.Timer.prototype */{
/**
* @constructs
*
* @param {Number} precision precision in milliseconds
*/
initialize: function(precision) {
xq.addToFinalizeQueue(this);
this.precision = precision;
this.jobs = {};
this.nextJobId = 0;
this.checker = null;
},
finalize: function() {
this.stop();
},
/**
* starts timer
*/
start: function() {
this.stop();
this.checker = window.setInterval(function() {
this.executeJobs();
}.bind(this), this.precision);
},
/**
* stops timer
*/
stop: function() {
if(this.checker) window.clearInterval(this.checker);
},
/**
* registers new job
*
* @param {Function} job function to execute
* @param {Number} interval interval in milliseconds
*
* @return {Number} job id
*/
register: function(job, interval) {
var jobId = this.nextJobId++;
this.jobs[jobId] = {
func:job,
interval: interval,
lastExecution: Date.get()
};
return jobId;
},
/**
* unregister job by job id
*
* @param {Number} job id
*/
unregister: function(jobId) {
delete this.jobs[jobId];
},
/**
* Execute all expired jobs immedialty. This method will be called automatically by interval timer.
*/
executeJobs: function() {
var curDate = new Date();
for(var id in this.jobs) {
var job = this.jobs[id];
if(job.lastExecution.elapsed(job.interval, curDate)) {
try {
job.lastReturn = job.func();
} catch(e) {
job.lastException = e;
} finally {
job.lastExecution = curDate;
}
}
}
}
});