So for something like this:
Jquery:
$(document).ready(function(){
var container = $('.rotate_container');
var images = container.find('img');
container.everyTime(10, function (){
images.each(function() {
container.animate({scrollTop: $(this).offset().top - 165}, 2000).delay(1000);
});
});
});
What’s the point of making the variables “container” and “images”? If they’re each only used once, how does that help anything at all?
thanks.
If you, for sure, only access an element once in your code, it does not make sense to cache it of course.
The reason for caching
DOM elementsis that it’s awful expensive to access them.Crossing the
bridgebetween ECMAland and DOMland is the most expensive part. You really can compare it with a bridge, each time you cross it you have to pay a toll. So it makes sense to keep the stuff in ECMAland for which you already payed a toll. Do not cross the bridge each time you need it.And in this picture you’ll notice that it does not matter, if you only cross the bridge once.