Commit 440c1711 authored by mcmlxxix's avatar mcmlxxix
Browse files

added emulated event emitter/handler adapter (applies event handling and...

added emulated event emitter/handler adapter (applies event handling and emitting prototypes to objects)
parent 11b8c339
......@@ -30,7 +30,7 @@
//will print "hello" infinitely at 20 second intervals
var event2 = timer.addEvent(20000,true,print,"hello");
as
//will print "hello" once after 30 seconds
var event3 = timer.addEvent(30000,false,
......@@ -82,13 +82,13 @@ function Timer() {
/* create a new event, do not include () on action parameter */
this.addEvent = function(interval,repeat,action,arguments,context) {
var event=new Event(interval,repeat,action,arguments,context);
var event=new TimerEvent(interval,repeat,action,arguments,context);
this.events.push(event);
return event;
}
/* event object created by addEvent */
function Event(interval,repeat,action,arguments,context) {
function TimerEvent(interval,repeat,action,arguments,context) {
/* last time_t at which event was executed */
this.lastrun = Date.now();
/* seconds between event occurance */
......@@ -117,4 +117,22 @@ function Timer() {
}
};
/* event emitter/handler adapter */
function Event() {
this.adapt = function(object) {
object.prototype.__events__ = {};
object.prototype.on = function(event,handler) {
this.__events__[event.toUpperCase()] = handler;
}
object.prototype.emit = function(event,args) {
args = Array.prototype.slice.call(arguments).slice(1);
if(typeof this.__events__[event.toUpperCase()] == "function") {
this.__events__[event.toUpperCase()](args);
}
}
}
};
//js.global.timer = new Timer();
//js.global.event = new Event();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment