Williammer
6/13/2014 - 8:33 AM

javaScript.closureTimer.js - use closure on the timer function, which repeat itself 100 times(similar to 100 times of different invocation)

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');