Javascript 如何知道Ajax调用何时使用getScript完成?

Javascript 如何知道Ajax调用何时使用getScript完成?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我使用jQuerygetScript将X个js文件加载到我的页面中。每个JS页面都有一个AJAX调用,用于从数据库中获取数据 我正在使用getScript上的.done方法查看所有文件的加载时间,但我需要等待所有AJAX调用完成 在执行某些操作之前,如何通过getScript等待AJAX调用在包含的文件中完成 $.getMultiScripts(moduleIncludes, 'includes/js/modules/').done(function() { // All of the

我使用jQuery
getScript
将X个js文件加载到我的页面中。每个JS页面都有一个AJAX调用,用于从数据库中获取数据

我正在使用
getScript
上的
.done
方法查看所有文件的加载时间,但我需要等待所有AJAX调用完成

在执行某些操作之前,如何通过
getScript
等待AJAX调用在包含的文件中完成

$.getMultiScripts(moduleIncludes, 'includes/js/modules/').done(function() {
    // All of the JS files have been included but we need to wait until those files have finished their AJAX call.     
});

// Get array of JS scripts to load
$.getMultiScripts = function(arr, path) {

  var _arr = $.map(arr, function(scr) {
      return $.getScript((path || "") + scr);
  });

  _arr.push($.Deferred(function(deferred) {
      $(deferred.resolve);
  }));

  return $.when.apply($, _arr);
}
你可以用

每当Ajax请求完成时,jQuery都会检查是否存在 任何其他未完成的Ajax请求。如果没有保留,jQuery将触发
ajaxStop
事件


另一种方法是使用
$.when.apply()
两次。也就是说,将ajax调用推送到第二个数组中

“module1.js”


plnkr

使所有程序都将其完成状态写入某个位置。然后,您需要查询“某处”(服务器、本地存储或其他),以检查是否所有过程都已完成。@threadp我如何等待或检查类似情况的状态?安装一个
setTimeout
处理程序,该处理程序既检查完成情况,又检查所有内容是否已完成(加载),执行进一步的代码。@threadp感谢您的建议。我会记住这一点,但会继续寻找一个更稳定的解决方案。如果你知道你可以使用计数器的文件数。这太酷了-我认为这将解决我的需要。
$(document).on("ajaxStop", function() {
  // do stuff when all `$.ajax()` call have complete
  $(document).off("ajaxStop")
});

$.getMultiScripts(moduleIncludes, 'includes/js/modules/').done(function() {
    // All of the JS files have been included but we need to wait until those files have finished their AJAX call.     
});
data.push($.get("/path/to/module1-resource"))
var modules = ["module1.js", "module2.js", "module3.js"];
// declare global `data` array
data = [];

$.getMultiScripts = function(arr, path) {

  var _arr = $.map(arr, function(scr) {
      return $.getScript(scr)
  });

  return $.when.apply($, _arr.concat($.when()))
}

$.getMultiScripts(arr)
.then(function() {
  console.log("getScript calls", arguments)
  $.when.apply($, data).then(function() {
    console.log("ajax calls", arguments)
  })
});