我試圖在腳本花費太長時間加載時測試我的網站的行爲。我知道如何完全阻止請求,以便通過向主機文件添加內容來返回404,但不知道如何強制請求不完整。如何使AJAX呼叫掛起
例,
127.0.0.1 translate.google.com # this blocks google translate from loading (404)
相反,我想提出translate.google.com 不是x秒響應。
我使用requirejs加載我的劇本,我注意到,超時行爲不同於404
我添加了一個模塊,允許谷歌翻譯進行選擇性要求,當我通過主機文件阻止它,該網站的行爲是正確的。腳本超時後,該站點無法加載。
// http://stackoverflow.com/questions/14164610/requirejs-optional-dependency
define("optional", [], {
load: function (moduleName, parentRequire, onload, config) {
var onLoadSuccess = function (moduleInstance) {
// Module successfully loaded, call the onload callback so that
// requirejs can work its internal magic.
onload(moduleInstance);
}
var onLoadFailure = function (err) {
// optional module failed to load.
var failedId = err.requireModules && err.requireModules[0];
console.warn("Could not load optional module: " + failedId);
// Undefine the module to cleanup internal stuff in requireJS
requirejs.undef(failedId);
// Now define the module instance as a simple empty object
// (NOTE: you can return any other value you want here)
define(failedId, [], function() { return {}; });
// Now require the module make sure that requireJS thinks
// that is it loaded. Since we've just defined it, requirejs
// will not attempt to download any more script files and
// will just call the onLoadSuccess handler immediately
parentRequire([failedId], onLoadSuccess);
}
parentRequire([moduleName], onLoadSuccess, onLoadFailure);
}
});
你能詳細說明你正在測試什麼嗎?您可以簡單地延遲在'onLoadSuccess'回調處理程序中使用'setTimeout((=)onload(moduleInstance),4 * 1000)'調用'onload'方法4秒延遲。 – alpeware
嘗試測試腳本超時發生的情況。當它返回404時,它的行爲是正確的,當它超時時,它行爲不正確。 – MPavlak