Prevent multiple web service calls via JQuery AJAX -


I'm starting to integrate some jQuery calls into my classic ASP pages, but many times Want to stop sending multiple server queries What is a good practical way of doing this?

It really depends on how your call looks.

For example, you can disable the button that sends the call using the before function. You can then re-enable it when you can get the success or error :

  $ ('# test'). ) {Var obj = $ (this); $ .ajax ({beforeSend: function () {toggleBtn (obj);}, url: '/ echo / html /', data: 'whatever', success: function (data) {// what data with whatever ... Warning ("When a warning is visible, JS is stopped, see the button. After clicking OK, it is being enabled."); ToggleBtn (obj);}, Error: Task () {// Whatever the error with ... // Re-enable the button, you need to do this, otherwise try again without error / / reload the user page And discover. ToggleBtn (obj);}}};});   

See one

Use a simple frame function to allow you to reuse the code:

  function Toggle BTN (OBJ) {If obj.attr ('disabled') == true) {obj.attr ('disabled', false); } And {obj.attr ('disabled' is true); }}   

Alternatively you can attach another click event:

  $ ('# test'). Click (function () ($ {AJAX ("url: '/ echo / html /', data: 'whatever, success: function (data) Showing a warning, JS has been stopped, after clicking OK, it is going to be enabled. ");},})}} .exext (function () {toggleBtn ($ (this)) ;)). AjaxStop (function {toggleBtn ($ (this));});   

See one

Another way is going when all the one Ajax call is going on. To do this, you can use all your AJAX buttons Then attach the document further:

  $ (document) .ajaxStart (function (e) {toggleBtn ($ ('. Ajax'))}}. AjaxStop (  

Note: The warning is to stop the execution of JS so that you can see what the button is actually disabled, otherwise it can be very quick to see what's going on.

Of course you can also add the option, which means that the script is still there It will not be the result unless it is the result.

Comments