的getScript docs說一下的是,成功回調:jQuery的getScript加入負載VS執行
「一旦腳本已經加載,但不一定執行的回調解僱了。」
但在我的測試中,似乎並非如此。對於與主機頁面:
var startTime = new Date();
$.getScript("test.js")
.done(function(script, textStatus) {
console.log(textStatus);
console.log("Done callback executing now.")
})
.fail(function(jqxhr, settings, exception) {
console.log("error.");
});
加載下面的「test.js」腳本擠佔UI 5秒:
console.log("ajaxed script starting to execute.");
var newTime = new Date();
while (newTime - startTime < 5000) {
newTime = new Date();
}
console.log("elapsed time", newTime - startTime);
console.log("ajaxed script finished executing.");
產生相同的預測的控制檯輸出在兩個FF &鉻:
ajaxed script starting to execute.
elapsed time 5000
ajaxed script finished executing.
success
Done callback executing now.
換句話說,成功的回調不會永遠火,直到加載腳本都加載和執行。這似乎是因爲在jQuery的source,則globalEval功能會立即調用腳本:
converters: {
"text script": function(text) {
jQuery.globalEval(text);
return text;
}
}
所以是文檔錯了嗎?如果他們是正確的,那麼在哪些特定情況下會成功回調激活之前腳本執行?
你可以把你的問題和減價到https://github.com/jquery/api.jquery.com/issues? :) – gnarf
該文檔是絕對錯誤的,謝謝你的領導:) –
謝謝,github問題提交:https://github.com/jquery/api.jquery.com/issues/420 – Ben