Created
June 8, 2011 22:13
-
-
Save kaaes/1015563 to your computer and use it in GitHub Desktop.
JS event loop example - uncomment alerts to see how blocking can stop whole app even if it is asynchronous & event based
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> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js" charset="utf-8" type="text/javascript"> | |
</script> | |
<title>Event Loop</title> | |
<style type="text/css"> | |
div {height: 5px; margin-bottom: 3px;} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
YUI().use('node', 'event-custom', function(Y) { | |
var create10divs = function(color) { | |
for(var i = 1; i < 10; i++) { | |
var d = Y.Node.create('<div style="background:'+color+'"><\/div>'); | |
Y.one('body').append(d) | |
} | |
} | |
Y.on('event1', function(){ | |
create10divs('red'); | |
Y.fire('event4'); | |
}) | |
Y.on('event2', function(){ | |
//alert('block!') | |
create10divs('green') | |
}) | |
Y.on('event3', function(){ | |
create10divs('blue') | |
}) | |
Y.on('event4', function(){ | |
create10divs('pink') | |
}) | |
Y.later(10, Y, function(){ | |
create10divs('orange') | |
}); | |
Y.on('domready', function(){ | |
//alert('block!') | |
create10divs('black') | |
}) | |
create10divs('purple') | |
var counter = 0; | |
var timer = Y.later(10, Y, function() { | |
counter++; | |
Y.fire('event'+counter); | |
if(counter === 3) { | |
timer.cancel(); | |
} | |
}, [], true); | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment