1

Why doesn't the following work? How do I click an element, hide it for a bit, and then show it? http://jsfiddle.net/ba8bJ/

$('#myButton').click(function() {$(this).hide().delay(800).show()});

4 Answers 4

3

hide() and show() only use the animation queue if a duration is specified.

You can provide a duration of 0 and simply write:

$("#myButton").click(function() {
    $(this).hide(0).delay(800).show(0);
});

You will find an updated fiddle here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Frederic. Ah, this is what I was missing.
2

Generally speaking, delay() is for animations. Use setTimeout() instead:

http://jsfiddle.net/rGqpn/

$('#myButton').click(function () {
    var $this = $(this).hide();
    setTimeout(function() {
        $this.show();
    }, 800);
});

1 Comment

Thanks Jason, I would think that hide/show limited animation, but evidently it isn't.
2
$('#myButton').click(function () {
    $(this).fadeOut(0).delay(800).fadeIn(0)
});

Delay only works for animations/queried functions

1 Comment

Thanks Spokey, This is probably what I will end up doing, even though Jason's answer more answered my misguided question.
1

Pass a duration to show() and hide() because when a duration is provided, .show() becomes an animation method.:

$('#myButton').on('click', function() {
    $(this).hide(0).delay(800).show(0);
});

JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.