简体   繁体   中英

jQuery - ignore double AJAX calls?

I have AJAX calls attached in multiple places (unfortunately not only buttons, but also links, forms and other stuff), I know how to handle this manually (find every place I do an AJAX call and then block / overlay the button during first call), I'm wondering if there's a way to do it more automagically?

If we're talking jQuery - maybe a plugin? Something that will just work? :)

It'd be perfect to have something like:

if clicked element has .ajax class 
  block all ajax requests if the current one is still live

I'd then add .ajax class to every button/link/whatever triggering the request and voila. Does anything like this exist?

You can to create a global variable:

loadingAjax = false;

whenever an event triggers, you turn this variable to TRUE by using:

$("selector").click(function(){
  if(!loadingAjax){
    loadingAjax = true;
    $.ajax(options)..
  }
});

And you should turn loadingAjax back into false when the ajax stops:

$( document ).ajaxStop(function() {
    loadingAjax = false
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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