jQuery 等待事件完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5563112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery wait for event to complete
提问by Florian Müller
How can I realize that when I have a function
我怎么能意识到当我有一个功能时
$('.anyclass').slideToggle(1000);
that the program exetutes the function test() after the slideToggle completed?
程序在slideToggle 完成后执行函数test() 吗?
If I write like this:
如果我这样写:
$('.anyclass').slideToggle(1000);
test();
...test() gets executed before slideToggle completed.
...test() 在 slideToggle 完成之前执行。
How can I realize? I don't know jQuery very well ...
我怎么能意识到?我不太了解 jQuery ...
Thank you for your help! Regards, Florian
感谢您的帮助!问候, 弗洛里安
EDIT: Results u can see here: www.virtual-swiss-hornets.ch
编辑:你可以在这里看到结果:www.virtual-swiss-hornets.ch
回答by The_Butcher
回答by zindel
I think slideToggle
should accept the second argument callback
, like this:
我认为slideToggle
应该接受第二个论点callback
,像这样:
$('.anyclass').slideToggle(1000, function() {/* do anything after animation is complete */});
Actually for your particular case it is as easy as:
实际上对于您的特定情况,它很简单:
$('.anyclass').slideToggle(1000, test);
回答by Lucas B
回答by James Kyburz
What you need is to use the callback function
你需要的是使用回调函数
http://api.jquery.com/slideToggle/
http://api.jquery.com/slideToggle/
.slideToggle( [ duration ], [ callback ] )
.slideToggle( [持续时间], [回调])
('.anyclass').slideToggle(1000, function() { test(); });
回答by Teuzer
The best way should be this
最好的方法应该是这个
$('#result').append($("<div class='message'>Result Sucess</div>")).slideToggle(5000,function(){$(this).remove()});
$('#result').append($("<div class='message'>Result Sucess</div>")).slideToggle(5000,function(){$(this).remove()});