Created
June 13, 2014 08:33
-
-
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.
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
| 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