Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I delay a remove call with jQuery

Tags:

I would like for a div to fadeOut and then be removed:

 $('#div').delay(1000).fadeOut(300);
 $('#div').delay(1300).remove();

Unfortunately, this just directly removes the div, with no delays. Why is it that I can't get the remove action to be delayed? What solutions are there?

Thanks

like image 313
jay Avatar asked Nov 11 '11 22:11

jay


People also ask

How to set delay in jQuery?

jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.

How do you delay onload function?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.

When to use setTimeout?

setTimeout() The setTimeout() function is used when you wish to run your JavaScript function after a specified number of milliseconds from when the setTimeout() method was called. where expression is the JavaScript code to run after the timeout milliseconds have elapsed.


1 Answers

If you want the element to be removed after it's done being faded out, you can fadeOut's callback parameter.

$('#div').delay(1000).fadeOut(300, function(){
   $(this).remove();
});
like image 110
Rocket Hazmat Avatar answered Sep 30 '22 13:09

Rocket Hazmat