Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay hide() from toggle()

I have an element that when clicked is either visible or hidden using jQuery's toggle() method.

Using toggle() is it possible to delay it from being hidden for a few seconds, while not delaying the visibility?

$('.myelement').click(function() {
    $('.myelement').toggle();
});
like image 638
Rich Avatar asked Jun 11 '26 09:06

Rich


1 Answers

Just try with:

$('.myelement').click(function() {
  if ($(this).is(':visible')) {
    $(this).delay(1000).hide();
  } else {
    $(this).show();
  }
});

Or simplier:

$('.myelement').click(function() {
  $(this).delay($(this).is(':visible') ? 1000 : 0).toggle();
});
like image 105
hsz Avatar answered Jun 21 '26 08:06

hsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!