简体   繁体   English

我怎么能停止ajax请求(不要等到直到响应)?

[英]How can i stop ajax request (don't wait untill response come)?

If I use Ajax to send request and this request take long time ..... if I want to send anther request what should I do? 如果我使用Ajax发送请求,这个请求需要很长时间.....如果我想发送另一个请求,我该怎么办?

the current behaviour the second request (I did) waiting until the first request get with response. 当前行为第二个请求(我做)等到第一个请求得到响应。

NOTE : i want to do this behaviour on whole application (any new request execute immediately not wait the old one to be finished firstly) My application using (Ajax + PHP + jQuery + Symfony) 注意:我想在整个应用程序上执行此行为(任何新请求立即执行而不是等待旧的首先完成)我的应用程序使用(Ajax + PHP + jQuery + Symfony)

Assume that is the first request take long time: 假设这是第一个请求需要很长时间:

$.ajax
({
    type: "GET",
    url: url1,
    success: function (html)
    {
       // do some thing  
    }
});

In any time I want this request to execute and terminate the first one. 在任何时候我都希望这个请求执行并终止第一个请求。

$.ajax
({
    type: "POST",
    url: url,
    success: function (html)
    {
       // do some thing else 
    }
});

var xhrReq;
xhrReq = $.ajax(...);

// then if you want to stop the rqest and exit use :

 xhrReq.abort();

It's sort of a manual process, but you can add a global xhr object and test it on each request. 这是一种手动过程,但您可以添加全局xhr对象并在每个请求上对其进行测试。 If the readystate is "loading", abort it: 如果readystate是“正在加载”,则中止它:

var xhr;
var loadUrl = function(url) {
    if ( xhr && xhr.readyState > 0 && xhr.readyState < 4 ) {
        // there is a request in the pipe, abort
        xhr.abort();    
    }
    xhr = $.get(url, function() {
        console.log('success', this);
    });
};

loadUrl('/ajax/');

The XMLHttpRequest object has an abort function. XMLHttpRequest对象具有中止功能。 You can use setTimeout to abort a request that is taking too long. 您可以使用setTimeout中止占用时间过长的请求。

EDIT: In the case you do not want to use a timer, and a new event occurs that should abort the prior request, then the event handler should do the following 编辑:如果您不想使用计时器,并且发生应该中止先前请求的新事件,那么事件处理程序应该执行以下操作

if(!this.request) return;  // request contains the XMLHttpRequest
this.request.onreadystatechange = function() {};
if(this.request.readyState != 4) {
    this.request.abort();
}

Then after that you can create the new XMLHttpRequest object. 然后,您可以创建新的XMLHttpRequest对象。

I have been working on this many ways and I feel I found a working solution. 我一直致力于这方面的工作,我觉得我找到了一个有效的解决方案。 I had a caching process that was causing a page to hang until done (average 5 seconds). 我有一个缓存过程导致页面挂起,直到完成(平均5秒)。 Yes this is better suited as a CRON job, but I needed to create caching process for the user without knowing the environment they are using for my CMS. 是的,这更适合作为CRON工作,但我需要为用户创建缓存过程,而不需要知道他们用于CMS的环境。 What I had done: Create the call within a variable and then remove it by a hard delete. 我做了什么:在变量中创建调用,然后通过硬删除删除它。 By deleting this it seems to be removing the wait. 通过删除它似乎正在删除等待。 This "hack" seemed to pull the wait from 5 second average to a 325ms wait. 这种“黑客”似乎将等待从5秒平均拉到325毫秒等待。

var ignore = $.ajax({
    url:something/here.php,
    type: "GET",
    url: url1,
    success: function(){}
});
delete ignore;

Defining the ajax request variable: 定义ajax请求变量:

var xhr;

Making the ajax call: 进行ajax调用:

xhr = $.ajax(...);

Aborting the ajax call: 中止ajax调用:

xhr.abort();

Browser allows you to handle only limited amount of requests to same host at time (2 or 3 as I remember, depending on browser). 浏览器允许您每次只处理对同一主机的有限数量的请求(我记得2或3,具体取决于浏览器)。

Workaround on requests count is to make fake domains - like img1.domain.com, img2.domain.com, etc. leading to the same host and randomly use them in requests. 请求计数的解决方法是制作虚假域名 - 例如img1.domain.com,img2.domain.com等,导致同一主机并在请求中随机使用它们。 Then you can just make requests you need. 然后你可以提出你需要的请求。 Domains count should be chosen depending on requests quantity in order to keep in bounds - 2 requests per domain. 应根据请求数量选择域计数以保持边界 - 每个域2个请求。 Otherwise 3rd request will wait until one of active finishes. 否则,第三个请求将等待,直到其中一个活动完成。

It allows you to receive responses from all your requests. 它允许您接收来自所有请求的回复。 For example, Google uses it to make images load faster. 例如,Google使用它来加快图片加载速度。

EDIT: 编辑:

Example: you have http://yourhost.com/ and alias http://alias.yourhost.com which points to the same place. 示例:您有http://yourhost.com/和别名http://alias.yourhost.com ,它们指向同一个地方。 Then: 然后:

$.ajax
({
    type: "GET",
    url: 'http://yourhost.com/somescript.php',
    success: function (html)
    {
       // do some thing  
    }
});

and then 接着

$.ajax
({
    type: "POST",
    url: 'http://alias.yourhost.com/somescript2.php',
    success: function (html)
    {
       // do some thing else  
    }
});

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

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