Jitender
Jitender

Reputation: 7969

Call back function with delay using jquery

Hi I use jquery call back function to removeClass, Now I want to attach delay with this function. My function is

$(function(){
$('a').click(function(){
$('.mydiv').find('.div1').addClass('jmnew').show('slow', function(){
$('.mydiv').find('.jmnew').removeClass('jmnew');
});
});
});

HTML//

<div class="mydiv">
<div class="div1" style="display:none">
abc
</div>
<a href="#">click me</a>
</div>

fiddle link

Upvotes: 0

Views: 2688

Answers (2)

Ashish Bhagat
Ashish Bhagat

Reputation: 420

You should use setTimeout

$(function(){
    $('a').click(function(){
        $('.mydiv').find('.div1').addClass('jmnew').show('slow', function(){
            setTimeout(function() {
                $('.mydiv').find('.div1').removeClass('jmnew')
            },2000);
        });
    });
});

Upvotes: 2

Michal Klouda
Michal Klouda

Reputation: 14521

You will need to use setTimeout function, since jQuery's delay() only works with queued effects.

$(function() {

    $('a').click(function() {
        $('.mydiv').find('.div1').addClass('jmnew').show('slow', function() {

            setTimeout(function() {
                $('.mydiv').find('.jmnew').removeClass('jmnew')
            }, 1000);
        })
    })
})​

See the DEMO.

Upvotes: 0

Related Questions