Skip to content

Instantly share code, notes, and snippets.

@jbgutierrez
Created March 19, 2012 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgutierrez/2107616 to your computer and use it in GitHub Desktop.
Save jbgutierrez/2107616 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Events Flow</title>
<style type="text/css" media="screen">
div {
background-color: #ddd;
padding: 40px;
border: 1px solid white;
}
div:hover {
cursor: pointer;
}
</style>
</head>
<body>
<div>
<div>
<div>
</div>
</div>
</div>
<div>
<div>
<div>
</div>
</div>
</div>
<script type="text/javascript" charset="utf-8">
function capturing(event){
var that = this;
publish(function(){
that.setAttribute('style', 'background-color: #eee');
console.log("capturing");
});
}
function bubbling(event){
var that = this;
publish(function(){
that.setAttribute('style', '');
console.log("bubbling");
});
}
var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
var e = elements[i];
e.addEventListener('click', capturing, true);
e.addEventListener('click', bubbling, false);
}
var lambdas = [];
function publish(lambda){
lambdas.push(lambda);
if (lambdas.length === 1) {
var timer = setInterval(function(){
lambdas.shift().call();
if (lambdas.length === 0) {
clearInterval(timer);
}
}, 200);
}
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment