This jQuery code loops through rows in a table and highlights each row as it goes. I have a stop button that sets a variable to false.
I know that I can break out of the .each loop by using return false;, but I want it to break out from within the queue.
How can I do this if the var is false?
$('.js-channel-notes tr').each(function(i) {
$(this).delay((i++) * 160).queue(function() {
$('.channel-row-highlight').removeClass('channel-row-highlight');
$(this).addClass('channel-row-highlight').clearQueue();
});
});
.queue()doesn't actually run the function. It just adds it onto a queue, to be ran at a later point with.dequeue()..each(). The loop ends long before the first.queue()starts. Don't use a loop at all. Instead select the elements, set up the first.queue(), and then when it runs, have it call the next, all the while keeping track of which index you're on.breakstatement works for loops constructed using keywords likeforandwhile. Theeachfunction executes its supplied argument function for each element of a collection, and therefore abreakinside that function would not impact the iterator in theeachfunction itself.