Reputation: 127
This is a continuation from this post - :visible selector issue
Anyway, I was wondering if there is a way to detect if a particular code segment is run until it is completed before running a new line of code.
For example,
if(filterVal == 'all') {
jQuery('ul#portfolio li.hidden').fadeIn('slow').removeClass('hidden');
} else {
jQuery('ul#portfolio li').each(function() {
if(!jQuery(this).hasClass(filterVal)) {
jQuery(this).fadeOut('normal').addClass('hidden');
} else {
jQuery(this).fadeIn('slow').removeClass('hidden');
}
});
}
What I want to do is make sure that all the list-item elements are successfully fadeIn(display:block) and fadeOut(display:none) successfully before triggering the jPages function to create pagination.
Upvotes: 0
Views: 105
Reputation: 127
So I think I found out how to do it using .promise()
My code looks something like this:
jQuery('ul#portfolio li').promise().done(function()
{
jQuery('ul#portfolio li').each(function()
{
console.log(jQuery(this).attr('class') + '-' + jQuery(this).css('display'));
});
});
Upvotes: 0
Reputation: 12683
Give it a callback function. Try:
jQuery(this).fadeOut('normal', function(){
$(this).addClass('hidden');
})
If you have to do it many times, use:
jQuery('ul#portfolio li').each(function() {
if(!jQuery(this).hasClass(filterVal)) {
jQuery(this).addClass('hidden');
} else {
jQuery(this).removeClass('hidden');
}
});
$('ul#portfolio li.hidden').fadeOut('normal', function(){
//Finished
});
$('ul#portfolio li:not("hidden")').fadeIn('slow', function(){
//Finished
});
Upvotes: 1