I am going through a book on Javascript and tried one of its example of closure.
function animateIt(id) {
var elem= document.getElementById(id);
var tick=0;
var timer=setInterval(function() {
if (tick<100) {
elem.style.left = tick+"px";
elem.style.top=tick+"px";
tick++;
} else {
clearInterval(timer);
assert(tick==100, "Tick Accessed and has value:- "+ tick);
assert(elem, "element accessed");
assert(timer, "timer accessed")
}
}, 10);
}
animateIt(box);
but the function in timer doesn't have access to elem, tick etc. whereas the book says it should have access to the same. Please let me know.