简体   繁体   English

是否可以中止同步XmlHttpRequest?

[英]Is it possible to abort a synchronous XmlHttpRequest?

I have written a JavaScript function that asynchronously calls a web service using XmlHttpRequest. 我编写了一个JavaScript函数,它使用XmlHttpRequest异步调用Web服务。 I have been asked to make this function finish its work before the page is rendered. 我被要求在呈现页面之前使此功能完成其工作。

I thought I could make the AJAX request synchronous but I don't want this to make the page hang too long - I'd like to abort the request after, say, 1 second if a response isn't received. 我以为我可以让AJAX请求同步但我不希望这会让页面挂起太长时间 - 如果没有收到响应,我想在1秒之后中止请求。

Is it possible to abort a synchronous XmlHttpRequest? 是否可以中止同步XmlHttpRequest?

You can't: 你不能:

http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX sais: "Synchronous AJAX (Really SJAX -- Synchronous Javascript and XML) is modal which means that javascript will stop processing your program until a result has been obtained from the server. While the request is processing, the browser is effectively frozen. The browser treats the call like an alert box or a prompt box only it doesn't wait for input from the user, but on input by the remote server" http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX sais:“同步AJAX(真正的SJAX - 同步Javascript和XML)是模态的,这意味着javascript将停止处理您的程序,直到从服务器获得结果当请求正在处理时,浏览器被有效冻结。浏览器将呼叫视为警告框或提示框,只是它不等待来自用户的输入,而是等待远程服务器的输入“

Once the browser runs the sync request, it will wait until it will get a response. 一旦浏览器运行同步请求,它将等待它获得响应。

First of all, synchronous AJAX calls are evil because they are blocking the whole JavaScript browser engine (which you are aware of). 首先,同步AJAX调用是邪恶的,因为它们阻塞了整个JavaScript浏览器引擎(你知道)。

Is simply doing the call asynchronously and discarding the result if it arrives later than after a second is not enough? 只是异步进行调用并丢弃结果,如果它晚于一秒后到达是不够的? If you really want to wait for the result you can still use setTimeout() (jQuery for convenience, not required): 如果你真的想等待结果,你仍然可以使用setTimeout() (jQuery是为了方便,不是必需的):

var result;
var xhr = $.ajax('/url', function(response) {
  result = response;
});
setTimeout(function() {
  if(result) {
    //AJAX call done within a second, proceed with rendering
  } else {
    //One second passed, no result yet, discard it and move on
    xhr.abort();
  }
}, 1000);

With this approach you are not blocking the browser while still you don't have to wait for the AJAX call. 使用这种方法,您不必阻止浏览器,而您不必等待AJAX​​调用。

XMLHttpRequest support abort method, you can get more details about it here: http://www.w3.org/TR/XMLHttpRequest/#the-abort-method XMLHttpRequest支持abort方法,你可以在这里获得更多关于它的细节: http//www.w3.org/TR/XMLHttpRequest/#the-abort-method

But you need to check how many browserы support it. 但您需要检查有多少浏览器支持它。 For example, abort was introduced in Windows Internet Explorer 7 and above. 例如,在Windows Internet Explorer 7及更高版本中引入了abort

Before or after calling send() method of XMLHttpRequest object you can setup a timer for n-seconds delay which will interrupt an asynchronous operation which is in progress. 在调用XMLHttpRequest对象的send()方法之前或之后,您可以设置一个n秒延迟的计时器,这将中断正在进行的异步操作。

It is possible in IE.Use the timeout property. 它可以在IE中使用。超时属性。

the code below worked for me 下面的代码对我有用

xmlhttp.open("method","url",false);
xmlhttp.timeout="time in ms";
xmlhttp.ontimeout=function(){};
xmlhttp.send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM