多个Ajax请求烧成后回调完成

编程入门 行业动态 更新时间:2024-10-23 17:31:33
本文介绍了多个Ajax请求烧成后回调完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 loadInfo: function(){ var jsonCounter = 0, room = ['room1','room2','room3'], dates = [], prices = [] $.each(booking.rooms, function(key, room_name) { $.getJSON('/get_info.php?room='+room_name, function(data) { dates[room_name] = data jsonCounter++ }) $.getJSON('/get_info.php?room='+room_name+'&prices', function(data) { prices[room_name] = data jsonCounter++ }) }) function checkIfReady() { if (jsonCounter === rooms.length * 2) { clearInterval(timer) run_the_rest_of_the_app() } } var timer = setInterval(checkIfReady, 100) }

(修改了很多,因为它是一类等等等等的一部分。)

(Modified a lot, as it's part of a class etc etc.)

目前,这感觉有点hackish,作为定时器的使用似乎垃圾。我会用$。当和$ .done,但我不知道有多少间可能有,所以我不知道该怎么投入的时候。

At the moment this feels a bit hackish, as the timer usage seems rubbish. I would use $.when and $.done, but I don't know how many rooms there might be, so I don't know what to put into when.

如何在AJAX请求我保证run_the_rest_of_the_app()仅被调用一次全部回来了?

How do I ensure that run_the_rest_of_the_app() only gets called once all of the AJAX requests come back?

推荐答案
  • VAR activeAJAX = 0;

在做一个AJAX调用 activeAJAX ++;

Before making an AJAX call, activeAJAX++;

在完成一个AJAX调用(回调)后:如果(--activeAJAX == 0){allDone(); }

After completing an AJAX call (in the callback): if (--activeAJAX == 0) { allDone(); }

更多推荐

多个Ajax请求烧成后回调完成

本文发布于:2023-11-25 04:04:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   烧成   回调   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!