Reputation: 14659
$('#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?
Upvotes: 1
Views: 3758
Reputation: 38428
$('#cart > .heading a').hover(function() {
// clear the previously defined timeout if any
clearTimeout($(this).data('timeout'));
$(this).addClass('active');
// do other stuff here
}, function() {
// set timeout and save it in element's state
// so it could be removed later on if needed
var e = $(this).data('timeout', setTimeout(function() {
e.removeClass('active');
}, 3 * 1000)); // 3 sec.
});
This way .removeClass('active') will take action only if mouse is located outside of the element.
Upvotes: 3
Reputation: 20371
You could just use setTimeout()
$('#cart').bind('mouseleave', function() {
var $that = $(this);
setTimeout(function(){$that.removeClass('active');}, 500); //500 millisec delay
});
Upvotes: 4