我想在RequireJS加载javascript库时调用一个函数。我用它来指示用户的初始初始化。 通常只需不到一秒钟,但如果需要更长时间,用户最终会盯着空白屏幕并认为Web应用程序出现故障。我想象这样的画面:
我可以抓住失败:
requirejs.onError = function(error) {
error.requireModules.forEach(function(name) {
//Create basename in case this was an URL
name.replace(/^.*[/]/, "");
// Debug
console.warn("Module failed to load: ", name);
// Find the loading node (they are hardcoded) and
// if it exists mark it as failed
var element = document.getElementById("loading_"+name);
if(element) {
element.className+=" failed";
}
});
}
所以我可以在该图像上准备弹出窗口并报告其中的失败。但报告成功也很重要。现在有一个选项我不想使用:
var require = {
deps: ["some/module1", "my/module2", "a.js", "b.js"],
callback: function(module1, module2) {
//This function will be called when all the dependencies
//listed above in deps are loaded. Note that this
//function could be called before the page is loaded.
//This callback is optional.
}
};
由于:
我想要的是与<{1}} 相对的。每次成功加载模块时调用回调。它也可用于调试目的。