I have an $image that I .fadeIn and .fadeOut, and then .remove after .fadeOut completes. This is my code:
$image .fadeIn() .fadeOut(function() { $(this).remove(); }); I want to add a .delay after .fadeOut, and .remove the $image only once .delay has completed. I have tried:
$image .fadeIn() .fadeOut() .delay(1000, function() { $(this).remove(); }); The problem is that .delay doest not accept a callback function. How can I .remove the picture as a callback to .delay?
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.
jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.
You can use the queue() method to schedule your own function to run after delay() completes:
$image.fadeIn() .fadeOut() .delay(1000) .queue(function(next) { $(this).remove(); next(); });
You can always do it as:
$image .fadeIn() .fadeOut(function() { var self = this; // Not sure if setTimeout // saves the pointer to this setTimeout(function() { $(self).remove(); }, 1000) });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With