Search code examples
jquerydelay

Jquery Delay Event


$('#cart > .heading a').bind('mouseenter', function() {
    $('#cart').addClass('active');


    $.ajax({
        url: 'index.php?route=checkout/cart/update',
        dataType: 'json',
        success: function(json) {
            if (json['output']) {
                $('#cart .content').html(json['output']);
            }
        }
    });         

    $('#cart').bind('mouseleave', function() {
        $(this).removeClass('active');
    });
});

I need to delay the removeClass on mouseleave. Can I simple add a this.delay line?


Solution

  • You could just use setTimeout()

    $('#cart').bind('mouseleave', function() {
        var $that = $(this);
        setTimeout(function(){$that.removeClass('active');}, 500); //500 millisec delay
    });