Created
September 1, 2011 17:17
-
-
Save mpfund/1186682 to your computer and use it in GitHub Desktop.
A Costum-Event-Object with Async/sync firing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cEvent = function(){ | |
this.eventh = []; | |
this.regEvent = function(eventh){ | |
if (typeof(eventh)==='function') | |
{ | |
this.eventh.push(eventh); | |
return true; | |
} | |
return false; | |
}; | |
this.fire = function(){ | |
var f = this.fireFac(); | |
return f(); | |
}; | |
this.fireFac = function(){ | |
var me = this; | |
var fire = function(){ | |
if (me.eventh.length<=0){return false;} | |
for (var x=0;x<me.eventh.length;x++) | |
{ | |
me.eventh[x](); | |
} | |
}; | |
return fire; | |
}; | |
this.asyncFire = function(){ | |
var f = this.asyncFac(); | |
return f(); | |
}; | |
this.asyncFac = function(){ | |
var me = this; | |
var afire = function(){ | |
if (me.eventh.length<=0){return false;} | |
for (var x=0;x<me.eventh.length;x++) | |
{ | |
me.callAsync(me.eventh[x]); | |
} | |
}; | |
return afire; | |
}; | |
this.callAsync = function(f){ | |
var i = null; | |
var asyncHelper = function(){ | |
f(); | |
clearInterval(i); | |
}; | |
i = setInterval(asyncHelper,0); | |
}; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Costume Sync/Async Events</title> | |
</head> | |
<body> | |
<script type="text/javascript" src="costumEvents.js"></script> | |
<script> | |
/* a wait function - loops until waittingtime is reached */ | |
var wait = function(waittime){ | |
var start = new Date(); | |
var stop; | |
var y=0; | |
do{ | |
for(var x = 0;x<1e6;x++){y+=x+x-3;} | |
stop = new Date(); | |
} | |
while(stop-start<waittime); | |
}; | |
/* a delay function. it uses the wait() function, | |
* waits for 1sec and prints the start/stop time */ | |
var delay = function(){ | |
var start = new Date(); | |
wait(1000); | |
console.log('start:'+start+" stop: "+new Date()); | |
}; | |
// Create a new costume Event | |
var t1 = new cEvent(); | |
// Register a function to the Event | |
t1.regEvent(delay); | |
// Benchmark 10 delay() runs | |
var start = new Date() | |
for(var x = 0; x < 10; x++){ | |
t1.asyncFire(); | |
} | |
console.log("Duration: "+ (new Date() - start)); | |
/* Normally the for-loop is passed in 0-2 milliseconds. -> No Browser Blocking ! | |
* and every second a new start/stop-time-line is written to the console. | |
* Replace asyncFire() with fire() to see the difference! -> Blocks the | |
* Browser for 10 seconds*/ | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment