admin管理员组文章数量:1323182
I'm learing deferreds, and can not work out how/why this works:
<html>
<head>
</head>
<body>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<script src=".7.1/jquery.min.js"></script>
<script>
$("div").each(function (i) {
$(this).delay(1000 * i).fadeOut();
}).promise().done(function () { console.log("it's done"); });
</script>
</body>
</html>
In prarticular: why can I call promise after each? Why it's technically possible? I've console.logged:
console.log($("div").each(function (i) {}));
and I do see promise method in prototype. But, my each function do not return any values, there's simply a :
$(this).delay(1000 * i).fadeOut();
So how promise is connected to animation result? (basically each iteration's result)
EDIT 1: Just to make it clear, perhaps I should have written my question like this:
How it's done that done method is called after all animations are finished == how promise is interconnected to animation that is executed inside the each callback function.
I'm learing deferreds, and can not work out how/why this works:
<html>
<head>
</head>
<body>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$("div").each(function (i) {
$(this).delay(1000 * i).fadeOut();
}).promise().done(function () { console.log("it's done"); });
</script>
</body>
</html>
In prarticular: why can I call promise after each? Why it's technically possible? I've console.logged:
console.log($("div").each(function (i) {}));
and I do see promise method in prototype. But, my each function do not return any values, there's simply a :
$(this).delay(1000 * i).fadeOut();
So how promise is connected to animation result? (basically each iteration's result)
EDIT 1: Just to make it clear, perhaps I should have written my question like this:
How it's done that done method is called after all animations are finished == how promise is interconnected to animation that is executed inside the each callback function.
Share Improve this question edited Jan 19, 2013 at 21:13 hohner 11.6k8 gold badges50 silver badges85 bronze badges asked Jan 19, 2013 at 14:05 dragonflydragonfly 17.8k31 gold badges114 silver badges227 bronze badges2 Answers
Reset to default 4I've never seen each
being used with promises, but only animation
$('div')
.animate({opacity: 0}, 1500) // animate all divs at once
.promise()
.done(function(){
console.log('all done');
});
this will animate all divs at once, then execute the callback when all divs finished animating.
the problem with animate inside a looping is that it can't be coordinated and you won't be able to control when they all finished if you don't use promises. If you really want to use each
, you'd have to make an array of promises
, then use then
var promises = [];
$('div').each(function(){
var $this = $(this);
promises.push($this.fadeOut('slow').promise());
});
$.when.apply($, promises).then(function(){
console.log('all done');
});
This isn't the same as doing $('div').fadeOut('slow', function(){ alert('done'); });
because the callback will happen for EACH element animated, while the promises deal like it was a "task" with many subtasks
http://jsfiddle/LbHrQ/3/
The best use of promises is when you want to synchronize some asynchronous operations by nature, like animations, ajax, things that use timeouts (in this case, you need to resolve()
your Deferred manually)
Have you tried adding the promise()
method inside the block? At the moment you're only executing it after all the iteration has finished.
$("div").each(function (i) {
$(this).delay(1000 * i).fadeOut().promise().done(function () { console.log("This individual animation is done."); });
}).promise().done(function () { console.log("Everything is done."); });
Your selector div
actually refers to quite a few elements on your page. As it iterates through them all, it executes a specific action to this element.
What you're then doing is asking jQuery to execute another action when all previous actions tied to those elements have finished. According to their documentation, it returns an object:
when all actions of a certain type bound to the collection, queued or not, have finished.
So, after every fadeOut()
has executed in the collection, the console.log()
is activated.
本文标签: javascriptDeferreds on jQueryeach resultStack Overflow
版权声明:本文标题:javascript - Deferreds on jQuery.each result - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742131921a2422200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论