Skip to content

Instantly share code, notes, and snippets.

@Williammer
Created June 13, 2014 08:33
Show Gist options
  • Select an option

  • Save Williammer/242b578a5c558fa64e88 to your computer and use it in GitHub Desktop.

Select an option

Save Williammer/242b578a5c558fa64e88 to your computer and use it in GitHub Desktop.
javaScript.closureTimer.js - use closure on the timer function, which repeat itself 100 times(similar to 100 times of different invocation) to modify the private value.
function animateIt(elementId) {
var elem = document.getElementById(elementId);
var tick = 0;
var timer = setInterval(function () {
if (tick < 100) {
elem.style.left = elem.style.top = tick + "px";
tick++;
} else {
clearInterval(timer);
assert(tick == 100,
"Tick accessed via a closure.");
assert(elem,
"Element also accessed via a closure.");
assert(timer,
"Timer reference also obtained via a closure.");
}
}, 10);
}
animateIt('box');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment