Friday, 13 September 2013

jquery kill long-running .each function

jquery kill long-running .each function

I'm using this to sequentially fadeIn a stack of div's:
$('#headerwrapper div').each(function(i) {
$(this).delay(999 + (i * 999)).fadeIn();
});
http://jsfiddle.net/Wkjdd/3/
I'm trying to figure out how to stop it via .scroll. (Pause/resume would
be nice, but after searching around and trying dozens of things, I'm ready
to let go of wanting that much.)
Here's the scroll function:
$(window).scroll(function(){
if ($(this).scrollTop() > 20) {
$('#headerwrapper').fadeOut();
} else {
$('#headerwrapper').fadeIn();
}
});
The reason it seems so important to be able to kill the .each function is
the finished product is going to be loading dozens of images, and to me,
if those aren't even in the viewport the function should just stop so as
to free up processing/memory.
I have no idea is .fadeOut of the parent div is enough to actually
stop/kill the .each function, or if it's still firing off continuously in
the background for nothing. What's a more correct way to kill (or
pause/resume if you're feeling generous) this .each function?

No comments:

Post a Comment