如何停止先前在jquery中发送的ajax调用 [英] how to Stop previous ajax call sent in jquery

查看:110
本文介绍了如何停止先前在jquery中发送的ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails 2中工作 我在如何停止在同一元素上的先前ajax调用方面遇到问题.

i am working in rails 2 I have an issue in how to stop previous ajax call on the same element.

例如,

我有类别下拉列表,该下拉列表会根据博客类型进行更新. 而且我已经两次单击了type,因此正在发送2个请求.在我的第二个请求结束之前,如果我已经根据加载子类别的第一个响应单击了类别,现在第二次加载了类别的响应就结束了并更新了类别.但是子类别是从第一个响应类别中选择的一个类别的响应.

i have categories dropdown which gets updated based on the blog type. And i have clicked type twice , so 2 requests are being sent. Before my second req got over, if i have clicked on the category based on the first response to load sub categories and now the second response to load the categories got over and updated the categories. but the subcategories are the response of one of the category selected from the first response categories.

如何停止上一个发送的ajax调用以获取类别.

How to stop the previous ajax call sent to fetch the categories.

推荐答案

var currentRequest = null;

function someFunction(target){
    currentRequest = jQuery.ajax({
       url: target,

       beforeSend : function()
        {           
            if(currentRequest != null)
            {
                currentRequest.abort()
            }
        },

        success: function() { alert("done"); }
    });
}

当然不需要该功能,只需使用包含xhr-object的变量并在发送之前中止请求.

Of course the function isn't necessary, just use a variable containing the xhr-object and abort the request before sent.

这篇关于如何停止先前在jquery中发送的ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆