我有一个页面向服务器发送请求。然后,在服务器上,我们向第三方Web API发送同步请求。由于调用了Web API,因此我们无法完全控制服务器端处理将花费多长时间。因此,我想做的是编写一个JavaScript函数,该函数将中断此页面请求,并在很长一段时间后将用户转发到错误页面。所以我的JavaScript看起来会像这样。

function onSubmit() {
   var oneMinute = (60 * 1000);
   setTimeout(function(){ window.location.replace("http://errorPage.html"); }, oneMinute);
   return true;
}

问题在于,即使执行了超时方法,页面仍会在重定向到错误页面之前等待服务器的响应。有没有一种方法可以中断页面请求,以使它不等到收到响应后才重定向到错误页面?

最佳答案

您可以尝试执行此操作,不确定是否在所有浏览器中都可以使用。

function onSubmit() {
   var oneMinute = (60 * 1000);
   setTimeout(function(){
        if($.browser.msie){
            document.execCommand('Stop');
        }
        else{//Other browsers
            window.stop();
        }
        window.location.replace("http://errorPage.html");
   }, oneMinute);
   return true;
}

09-10 11:35
查看更多