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) }
(经过多次修改,因为它是类等的一部分等)
目前,这感觉有点hackish,因为计时器使用似乎是垃圾.我会使用$.when和$.done,但我不知道有多少房间,所以我不知道什么时候投入.
所有AJAX请求返回后,如何确保仅调用run_the_rest_of_the_app()?