Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery wait for event to complete

Tags:

jquery

How can I realize that when I have a function

$('.anyclass').slideToggle(1000);

that the program exetutes the function test() after the slideToggle completed?

If I write like this:

$('.anyclass').slideToggle(1000);
test();

...test() gets executed before slideToggle completed.

How can I realize? I don't know jQuery very well ...

Thank you for your help! Regards, Florian

EDIT: Results u can see here: www.virtual-swiss-hornets.ch

like image 436
Florian Müller Avatar asked Apr 06 '11 07:04

Florian Müller


2 Answers

I think slideToggle should accept the second argument callback, like this:

$('.anyclass').slideToggle(1000, function() {/* do anything after animation is complete */});

Actually for your particular case it is as easy as:

$('.anyclass').slideToggle(1000, test); 
like image 114
zindel Avatar answered Nov 11 '22 23:11

zindel


You can add a callback function to your jQuery function.

$('.anyclass').slideToggle(1000,function() { test(); });

That function will ONLY fire once the slide is complete.

like image 25
The_Butcher Avatar answered Nov 11 '22 23:11

The_Butcher