views:

67

answers:

2

How do I use the JQuery delay in conjunction with my own defined function, pseudo-code like so?:

$('#foo').slideUp(300).delay(800).myOwnFunction(1,2,3);
function myOwnFunction (a,b,c) {...}

The code above doesn't work, however - per the JQuery documentation it appears like it should.

+3  A: 

Use setTimeout() here. After the animation has finished sliding up, it will run the anonymous function that wraps the setTimeout() which calls your function after approx 800 milliseconds.

$('#foo').slideUp(300, function() {

    setTimeout(function() {

       myOwnFunction(1, 2, 3);

    }, 800);

});




function myOwnFunction(a, b, c) {
       alert(a + b + c);
   };

It doesn't matter that this was defined below as it's definition should be hoisted to the top.

alex
Do I have to define myOwnFunction inside setTimeout?
Timj
@Timj Nope sorry, I fixed the example.
alex
awesome, works. many thanks.
Timj
check this article on the settimeout,setiterval will give you more ideahttp://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
Pranay Rana
A: 
$('#foo')
    .slideUp(300)
    .delay(800)
    .queue(function () {
        myOwnFunction(1,2,3);
        $(this).dequeue();
    });

See:

Jeffery To