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,448
  • 25
  • 78
  • 120

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,440
  • 2
  • 27
  • 38
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,837
  • 11
  • 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,591
  • 3
  • 28
  • 48
Lucas B
  • 11,793
  • 5
  • 37
  • 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
  • 13,775
  • 1
  • 32
  • 33
0

The best way should be this

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

Teuzer
  • 1