如何进行AJAX调用挂起

时间:2017-05-05 14:27:22

标签: javascript ajax testing requirejs timeout

我正在尝试在脚本加载时间过长时测试我的网站的行为。我知道如何完全阻止请求,以便通过向主机文件添加内容返回404,但不确定如何强制请求不完整。

实施例,

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);
    }
});

0 个答案:

没有答案