Skip to content

Instantly share code, notes, and snippets.

@sgentile
Created May 13, 2011 14:28
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 sgentile/970627 to your computer and use it in GitHub Desktop.
Save sgentile/970627 to your computer and use it in GitHub Desktop.
Basic Javascript Callback
<!-- This demonstrates a simple javascript callback -->
<div id="now">0</div>
<input type="button" id="goBtn" value="Go" />
<br/>
<div id="result">Let's go!</div>
<script>
var result = document.getElementById('result');
function animate(element, callback){
var left = -1;
function next(){
left++;
if(left < 350){
element.style.marginLeft = left + 'px';
element.innerHTML = element.style.marginLeft;
window.setTimeout(next, 5);
}
else{
callback();
}
}
next();
}
var btn = document.getElementById('goBtn');
btn.onclick = go;
function go(){
animate(document.getElementById('now'), function(){result.innerHTML = "done! try again?";});
result.innerHTML = "working...";
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment