在最后一项完成之前停止 Array.each 中的下一项?
我有一个 array.each 函数,它为每个项目调用 JSONP 请求,并将代码从 JSONP feed 注入到页面中。
然而,这些项目是以错误的顺序注入的,也就是与 feed 中的顺序不同。我假设发生这种情况是因为下一个项目在第一个完成检索其提要并注入之前通过 .each 函数运行。
如何强制每个“下一个”项目等待前一个项目完成?
var list = new Array("1","2","3","4","5");
Array.each(list, function(item)
{
new request.JSONP..
onComplete: function()
{
//inject code
}
});
如果注入代码输出数组中的数字,那么每次都是随机的:
2,4,5,1,3
我希望它按顺序排列:
1,2,3,4,5
希望这是有道理的,谢谢。
使用 Mootools 1.3
I have a array.each function that calls a JSONP request for each item and injects code into the page from the JSONP feed.
However the items are being injected in the wrong order, aka different to the sequential order in the feed. I'm assuming this happens because the next item it run through the .each function before the first once finishes retrieving its feed and injects.
How can I force each "next" item to wait untill the previous one has finished?
var list = new Array("1","2","3","4","5");
Array.each(list, function(item)
{
new request.JSONP..
onComplete: function()
{
//inject code
}
});
if the inject code output the numbers in the array it would be random every time:
2,4,5,1,3
I want it to be in order:
1,2,3,4,5
Hope that made sense, Thanks.
Using Mootools 1.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎来到事件驱动编程。
您需要从前一个的“onComplete”中触发每一个,而不是尝试从同一段代码中将它们全部触发。您可以通过让每个人等待直到您的 onComplete 设置标志来做到这一点,但最好根据事件构建代码。
Welcome to event-driven programming.
You need to fire each one off from the 'onComplete' of the one before, not try to fire them all off from the same piece of code. You could do that by making each one wait until a flag gets set by your onComplete, but it is much better to structure your code according to the events.