Xavian Tracy
Xavian Tracy

Reputation: 127

wait until certain code is run

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

Answers (3)

Xavian Tracy
Xavian Tracy

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

Akhil Sekharan
Akhil Sekharan

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

Marty
Marty

Reputation: 39456

There's a parameter in .fadeIn() that takes care of that.

$('ul#portfolio li.hidden').fadeIn('slow', function()
{
    // Fade has finished, continue here.
    //
    //

});

Upvotes: 2

Related Questions