43

what's wrong with that?

$('body').append("<div class='message success'>Upload successful!</div>");
$('.message').delay(2000).remove();

I want to append a success message to my html document, but only for 2sec. After that the div should be deleted again.

what am i doing wrong here?

regards

4 Answers 4

119

Using setTimeout() directly (which .delay() uses internally) is simpler here, since .remove() isn't a queued function, overall it should look like this:

$('body').append("<div class='message success'>Upload successful!</div>");
setTimeout(function() {
  $('.message').remove();
}, 2000);

You can give it a try here.

.delay() is for the animation (or whatever named) queue, to use it you'd have to do something like:

$("<div class='message success'>Upload successful!</div>").appendTo('body')
  .delay(2000).queue(function() { $(this).remove(); });

Which works, here...but is just overkill and terribly inefficient, for the sake of chaining IMO. Normally you'd also have to call dequeue or the next function as well, but since you're removing the element anyway...

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

8 Comments

+1 always providing good jquery answers similar to the one you provided for me earlier...
@Shog9 - To be completely accurate it's not just animations, it's just by default the fx queue animations run on, but it can be any queue if passed a name :)
Woops, deleted that comment since you updated your answer. But you're correct, the queue support is fairly general-purpose, and could be used for other things - the animations just default to it.
@Shog9 - I just hope .delay() gets some love for queue/animation integration in 1.5, so that a .stop(true, true) purges the timeout which isn't stored at all currently...gives some very weird and unexpected side-effects at the moment, something that's not advertised enough IMO.
@mattsoave if you wanted to create/remove a single one, you'd just keep a reference to it, like this: var m = $("<div class='message success'>Upload successful!</div>").appendTo("body"); and use m.remove(); instead.
|
9

I think that correct way of doing that is to use jQuery queue method:

    $("<div class='message success'>Upload successful!</div>")
        .appendTo('body')
        .delay(2000)
        .queue(function() {
            $(this).remove();
        });

Comments

2

Maybe I'm using an outdated jQuery, but none of the methods suggested in other answers seem to work for me. According to http://api.jquery.com/delay/ , delay is for animation effects.

Using setTimeout() however, works nicely for me:

$('body').append("<div class='message success'>Upload successful!</div>"); 
setTimeout(function(){
    $(".message").remove();
}, 2000);

1 Comment

Don't pass a string to setTimeout()!, pass an anonymous function :)
0

And just for kicks, you could do the following, using delay:

$('body').append("<div class='message success'>Upload successful!</div>");
$('.message').show('fast').delay(2000).hide('fast')
$('.message').remove();

2 Comments

@Shog9. You are right. I just had it working in a jsfiddle ... must have been a different snippet. Weird.
Happened to stumble across this question and answer when looking for setTimeout and saw that you are still active. No idea if you could do this back in 2010, but nowadays you can use a function as a second parameter in your hide function. So just for kicks you could do $('.message').show('fast').delay(2000).hide('fast', function(){$('.message').remove()}); =)

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.