jQuery: append() object, remove() it with delay()

前端 未结 4 1507
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 06:09

what\'s wrong with that?

$(\'body\').append(\"
Upload successful!
\"); $(\'.message\').delay(2000).remove(); <
4条回答
  •  广开言路
    2020-12-01 06:38

    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("
    Upload successful!
    "); 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:

    $("
    Upload successful!
    ").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...

提交回复
热议问题