Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使AJAX调用挂起_Javascript_Ajax_Testing_Requirejs_Timeout - Fatal编程技术网

Javascript 如何使AJAX调用挂起

Javascript 如何使AJAX调用挂起,javascript,ajax,testing,requirejs,timeout,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不同 我添加了一个模块,允许选择性地需要google tran

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

例如

127.0.0.1 translate.google.com # this blocks google translate from loading (404)
相反,我想让translate.google.com在x秒内不响应

我正在使用requirejs加载脚本,我注意到超时行为与404不同

我添加了一个模块,允许选择性地需要google translate,当我通过主机文件阻止它时,该站点的行为正常。脚本超时时,站点无法加载

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

你需要用一个你可以控制的网站来测试它。最简单的解决方案是使用您自己的localhost和一个等待几秒钟的小脚本

等待30秒的简短PHP示例如下所示:

<?php
  sleep(30);
  echo 'DONE';
运行本地(127.0.0.1是localhost的标准)并将以下.htaccess文件放入apache根目录(通常为“htdocs”):

然后将上面的PHP示例放在根目录中名为index.PHP的文件中(通常为“htdocs”)


然后您的本地服务器应该响应呼叫,您可以调整响应时间,直到超时。

您需要使用您控制的网站进行测试。最简单的解决方案是使用您自己的localhost和一个等待几秒钟的小脚本

等待30秒的简短PHP示例如下所示:

<?php
  sleep(30);
  echo 'DONE';
运行本地(127.0.0.1是localhost的标准)并将以下.htaccess文件放入apache根目录(通常为“htdocs”):

然后将上面的PHP示例放在根目录中名为index.PHP的文件中(通常为“htdocs”)


然后您的本地服务器应该响应呼叫,您可以调整响应时间,直到超时。

您可以详细说明您要测试的内容吗?您可以使用类似于
setTimeout(()=>onload(moduleInstance),4*1000)
的方法延迟调用
onLoadSuccess
回调处理程序中的
onload
方法4秒。尝试测试脚本超时时会发生什么。当它返回404时,它的行为是正确的,当它超时时,它的行为是不正确的。你能详细说明你要测试什么吗?您可以使用类似于
setTimeout(()=>onload(moduleInstance),4*1000)
的方法延迟调用
onLoadSuccess
回调处理程序中的
onload
方法4秒。尝试测试脚本超时时会发生什么。当它返回404时,它的行为是正确的,当它超时时,它的行为是不正确的。但是,我不希望将所有外部脚本移动到我的项目中,只是为了测试它们无法响应时会发生什么。只要应用程序在浏览器中运行,就不必这样做。您可以调整本地主机文件,以便将所需的请求重定向到本地主机(127.0.0.1)。为我不提供服务的脚本指向本地将返回404。这不好,我更新了我的答案,你可以如何避免404和服务您的本地超时脚本代替。希望能有帮助。我相信这会奏效的。但是,我使用的是C#和asp.net,而不是php和apache。你知道如何使用对堆栈更友好的东西来完成这项工作吗?从技术上讲,我可以做到。但是,我不希望将所有外部脚本移动到我的项目中,只是为了测试它们无法响应时会发生什么。只要应用程序在浏览器中运行,就不必这样做。您可以调整本地主机文件,以便将所需的请求重定向到本地主机(127.0.0.1)。为我不提供服务的脚本指向本地将返回404。这不好,我更新了我的答案,你可以如何避免404和服务您的本地超时脚本代替。希望能有帮助。我相信这会奏效的。但是,我使用的是C#和asp.net,而不是php和apache。您知道如何使用对该堆栈更友好的东西来完成此工作吗?
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^(.*)$ index.php [QSA,L]