Open up Open up - 1 year ago 45
jQuery Question

Does it consider recursion if I call onload event on the funcion which I'm inside?

I have this code:

function lazyCss(gallery) {
var images = gallery.slider.find('[data-lazy-image]'),
imageLinks = [],
imageToLoad = -1;

for (var i = 0; i < images.length; i++) {
imageLinks[i] = images.eq(i).data('lazy-image');
images.eq(i).removeAttr('data-lazy-image');
}

function loadImage() {
imageToLoad++;
images.eq(imageToLoad).css('background-image', 'url(' + imageLinks[imageToLoad] + ')');
if (imageToLoad != images.length - 1) {
$('<img/>').attr('src', imageLinks[imageToLoad]).on('load', loadImage);
}
}

loadImage();
}


My question is:

1) When I attach an event handler "loadImage" to an image, does it consider recursion because I'm doing it inside this function?

My worries that this code will be bad for performance on my website, since recursion is considered bad.

Answer Source

My questions is:

1) When I attach an event handler "loadImage" to an image, does it consider recursion because I'm doing it inside this function?

No, the JavaScript at Question does not perform a "recursion". You are scheduling a possible function call, not returning a value from a function call.

See

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download