Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback to .delay()

Tags:

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?

like image 442
Randomblue Avatar asked Oct 27 '11 11:10

Randomblue


People also ask

How do you delay a callback 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.

What is the use of delay () method in jQuery?

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

When callback function is executed?

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.


2 Answers

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();       }); 
like image 80
Frédéric Hamidi Avatar answered Sep 24 '22 18:09

Frédéric Hamidi


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)     }); 
like image 44
Lapple Avatar answered Sep 26 '22 18:09

Lapple