开发者

Kill jquery ajax form submit

开发者 https://www.devze.com 2023-02-12 23:59 出处:网络
I am remotely submitting a form potentially several times in close succession. For this particular case, debouncing is not an option. I am looking for the jQuery equivalent of .abort() for remote form

I am remotely submitting a form potentially several times in close succession. For this particular case, debouncing is not an option. I am looking for the jQuery equivalent of .abort() for remote forms submitte开发者_开发百科d using .submit() so I can cancel all previous submissions when a new one is made.

Thanks in advance, Garrett


What I did is attach to the beforeSend event of the form, store the xhr object as part of the form's data, and abort it if a new request is enqueued:

$('form').bind("ajax:beforeSend", function(evt, xhr) {
  console.log('Enqueued new xhr request');
  var prevXhr = $(this).data('current-xhr');
  if (prevXhr) {
    prevXhr.abort();
    console.log('Aborting previous xhr request');
  }
  $(this).data('current-xhr', xhr);
});

And you can still use the :remote => true option in the form straightforward.


Maybe you could use jQuery .queue() to queue your submits and .clearQueue() to clear the queue when needed. Just an idea...


I don't understand your problem. If the form is submitted, a request is sent to the server... so, how can you abort that?

If by abort you mean, cancelling the processing of the response, just use a control variable that you increase/decrease to know if there are pending requests.

From the API: http://api.jquery.com/category/ajax/ you can't do that.

0

精彩评论

暂无评论...
验证码 换一张
取 消