Rich
Rich

Reputation: 1156

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();
});

Upvotes: 2

Views: 1055

Answers (1)

hsz
hsz

Reputation: 152294

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();
});

Upvotes: 9

Related Questions