9

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

Florian Müller
  • 7,021
  • 25
  • 75
  • 119

5 Answers5

17

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.

The_Butcher
  • 2,420
  • 1
  • 25
  • 37
4

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); 
zindel
  • 1,767
  • 10
  • 13
2

If you only need this to fire once, Checkout the jquery API for when done

$.when(('.anyclass').slideToggle(1000)).done(function() { test(); });
vinayakj
  • 5,423
  • 3
  • 27
  • 47
Lucas B
  • 11,403
  • 5
  • 36
  • 50
1

What you need is to use the callback function

http://api.jquery.com/slideToggle/

.slideToggle( [ duration ], [ callback ] )

   ('.anyclass').slideToggle(1000, function() { test(); });
James Kyburz
  • 12,741
  • 1
  • 31
  • 33
0

The best way should be this

$('#result').append($("<div class='message'>Result Sucess</div>")).slideToggle(5000,function(){$(this).remove()});

Teuzer
  • 1