jQuery承诺在函数中每个() [英] jQuery promise in function with each()
问题描述
我试着调用一个自己的函数并等到它完成。转换结束后,我想开始下一个功能。
请看看我的jsfiddle http://jsfiddle.net/hrm6w/
console.log(上限完成)应该在console.log(promise finished)之后启动,并且所有动画都已结束。
在每个对象的所有动画结束后,我想开始下一个动作(函数)。
我认为promise()函数是我需要的,但我只是没有得到这个工作。
任何帮助将不胜感激。
玩了一下之后,似乎需要从转换中返回承诺。我修改了一下,但结果相同。
这篇文章帮助解释了一些概念: http://hermanradtke.com/2011/05/12/managing-multiple-jquery-promises.html
演示: http://jsfiddle.net/lucuma/ hrm6w / 5 /
(function($){
//转出
$ .fn.ajaxTransitionOut = function(){
var $ animators = $('。animate',this);
$ animators.each(function(){
$ (this).animate({
left:1000,
opacity:0
},400);
console.log('animating');
}) ;
return $ animators.promise()。done(function(){
console.log('promise finished');
});
};
})(jQuery);
$(document).ready(function(){
console.log('starting');
$ .when($('#content')。ajaxTransitionOut ))。done(function(){
console.log('upper finished');
});
console.log('ending');
});
I'm tryin to call a own function and wait until its finishes. After the transitions end I want to start the next function. please have a look in my jsfiddle http://jsfiddle.net/hrm6w/
The console.log("upper finished") should start after console.log("promise finished") and all animations have ended.
And after all animations in the each-Object have ended I want to start the next actions(functions).
I think the promise()-function is all I need, but I just doesn't get this working.
Any help would be appreciated.
After playing a bit it seems you need to return the promise from the transition. I modified it a bit but has the same result.
This article helped explain some of the concepts: http://hermanradtke.com/2011/05/12/managing-multiple-jquery-promises.html
Demo: http://jsfiddle.net/lucuma/hrm6w/5/
(function($) {
//Transition Out
$.fn.ajaxTransitionOut = function() {
var $animators = $('.animate', this);
$animators.each(function() {
$(this).animate({
left: 1000,
opacity: 0
}, 400);
console.log('animating');
});
return $animators.promise().done(function() {
console.log('promise finished');
});
};
})(jQuery);
$(document).ready(function() {
console.log('starting');
$.when($('#content').ajaxTransitionOut()).done(function() {
console.log('upper finished');
});
console.log('ending');
});
这篇关于jQuery承诺在函数中每个()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!