2010-06-02 46 views
10

我想知道在使用jquery和ajax时停止duplciate提交的最佳方法是什么?停止重复的Ajax子镜像?

我想出了2种可能的方式,但不知道它们是仅有的2

  1. 在阿贾克斯开始禁用所有按钮,直至请求完成。 2我遇到的问题虽然是我使用jquery模型对话框,所以我不知道如何禁用这些按钮,因为我不知道他们是否有id。第二,如果请求挂起,用户无法再次尝试,因为所有按钮都被禁用。

  2. 我正在寻找一种叫做AjaxQueue的功能,在这个时候,我不知道它是什么我需要的或者它是如何工作的,因为插件显然是停机维护的。

http://docs.jquery.com/AjaxQueue

编辑

我觉得这是关闭的我一直在寻找在旋转。

http://www.protofunc.com/scripts/jquery/ajaxManager/

我这个ajaxManager看到的唯一问题是,我想我有我所有的$。员额,$不用彷徨和$就那些改变自己的类型。

但是如果我需要$ .ajax中的特殊参数会发生什么?或者我喜欢使用.post和.get。

编辑2

我认为它可以在所有$阿贾克斯选项。我仍在研究它。然而,我现在不确定的是,我可以为所有使用相同选项的请求使用相同的构造函数。

First you have to construct/configure a new Ajaxmanager 
//create an ajaxmanager named someAjaxProfileName 
var someManagedAjax = $.manageAjax.create('someAjaxProfileName', { 
    queue: true, 
    cacheResponse: true 
}); 

或者我必须每次都做出上述情况吗?

回答

20

如何在用户点击按钮时设置标志?只有在AJAX请求成功完成时(complete,在successerror回调之后调用)才能清除标志,并且只有在未设置标志时才会发送AJAX请求。

与AJAX队列相关的是一个名为jQuery Message Queuing的插件,非常好用。我自己使用它。

var requestSent = false; 

jQuery("#buttonID").click(function() { 
    if(!requestSent) { 
     requestSent = true; 

     jQuery.ajax({ 
     url: "http://example.com", 
     ...., 
     timeout: timeoutValue, 
     complete: function() { 
      ... 
      requestSent = false; 
     }, 
     }); 
    } 
}); 

可以为长时间运行的请求超时值(值以毫秒为单位),如果你认为你的请求有挂的可能性。如果发生超时,则调用error回调,之后将调用complete回调。

9

您可以将活动请求存储在变量中,然后在出现响应时将其清除。

var request; // Stores the XMLHTTPRequest object 

$('#myButton').click(function() { 
    if(!request) { // Only send the AJAX request if there's no current request 

     request = $.ajax({ // Assign the XMLHTTPRequest object to the variable 
      url:..., 
      ..., 
      complete: function() { request = null } // Clear variable after response 
     }); 

    } 
}); 

编辑:关于这个

的一个好处是,你可以使用abort()取消长时间运行的请求。

var request; // Stores the XMLHTTPRequest object 
var timeout; // Stores timeout reference for long running requests 

$('#myButton').click(function() { 
    if(!request) { // Only send the AJAX request if there's no current request 

     request = $.ajax({ // Assign the XMLHTTPRequest object to the variable 
      url:..., 
      ..., 
      complete: function() { timeout = request = null } // Clear variables after response 
     }); 

     timeout = setTimeout(function() { 
         if(request) request.abort(); // abort request 
        }, 10000);       // after 10 seconds 

    } 
}); 
+0

不会使用'timeout'属性会更容易吗?还是不按照我认为的方式工作? :) – 2010-06-02 17:15:41

+0

@Vivin - 好点。除非您想在'setTimeout()'中使用其他逻辑来决定是否中止,否则可以使用'timeout'属性。 'request'变量是一个类似于你的答案的标志,但是如果需要的话,还可以提供'abort()'功能。 – user113716 2010-06-02 17:21:18

0
$.xhrPool = {}; 
$.xhrPool['hash'] = [] 
$.ajaxSetup({ 
    beforeSend: function(jqXHR,settings) { 
     var hash = settings.url+settings.data 
     if ($.xhrPool['hash'].indexOf(hash) === -1){ 
      jqXHR.url = settings.url; 
      jqXHR.data = settings.data; 
      $.xhrPool['hash'].push(hash); 
     }else{ 
      console.log('Duplicate request cancelled!'); 
      jqXHR.abort(); 
     } 
    }, 
    complete: function(jqXHR,settings) { 
     var hash = jqXHR.url+jqXHR.data 
      if (index > -1) { 
       $.xhrPool['hash'].splice(index, 1); 
      } 
     } 
    });