-1

I have a ajax call and it sends request every 10ms:

        function sendRequest(){
                    new Ajax.Request(getProgressUrl,
                    {
                          method: 'get',
                          onComplete: function(transport) {
                             setTimeout(updateCsvExportProgress, 10);
                          },
                          onFailure: function(xhr, json) {

                          }
                    });
        }

And there is button:

<button id="stopIt" onclick="stopAjaxRequest()">STOP</button>

I am wondering how can I stop/abort the ajax call since it is sending the request every 10 ms.

Thanks in advance.

Kurt Shaw
  • 609
  • 2
  • 10
  • 26
  • just setting a flag will also do if you want to stop the recurring call (don't call setTimeout based on the flag). To cancel a request already in progress, follow the link suggested by @Capsule. – gp. Mar 05 '15 at 03:26

1 Answers1

1

You should keep your Ajax.Request object in a variable & then run abort on that.

Your code should be like this:

var ajaxRequest = null;

function sendRequest() {
    ajaxRequest = new Ajax.Request(getProgressUrl, {
        method: 'get',
        onComplete: function(transport) {
            setTimeout(updateCsvExportProgress, 10);
        },
        onFailure: function(xhr, json) {

        }
    });
}

function stopAjaxRequest() {
   if(ajaxRequest !=null)
    ajaxRequest.transport.abort();
}
Apul Gupta
  • 3,044
  • 3
  • 22
  • 30