2

I have an ajax that run in background..

   var sTimeOut = function () {
        $.ajax(
        {
            type: "POST",
            url: '@Url.Action("checkJobIDs")',
            dataType: "json",
            async: true,
            success: function (data) {
        }
    });
}
var interval = 10000;
setInterval(sTimeOut, interval);

Searching an element I want to stop that another ajax which runs in background.

   $.ajax(
    {
        type: "POST",
        url: '@Url.Action("searchData")',
        dataType: "json",
        mtype: "post",
        data: { str: str },
        async: true,
        complete: function () { $(sTimeOut).hide(); },
        success: function (data) {
            search(data.searched[0]);
        }
    });

How can I prevent first ajax while working on 2nd ajax?

  • 2
    `async: false` ? but its terrible , use call back – NullPoiиteя May 05 '15 at 08:39
  • You can use `clearInterval` when the second ajax call takes place. – kidA May 05 '15 at 08:40
  • @NullPoiиteя but it doesnot affect –  May 05 '15 at 08:41
  • @kidA where I have to set `clearInterval` in ajax..after success or before it? –  May 05 '15 at 08:41
  • 1
    you can have a `beforeSend` in your second ajax call that will clear the interval. Check [this link](https://api.jquery.com/Ajax_Events/). – kidA May 05 '15 at 08:44
  • @kidA thnx its working..but will it start working after completing searching? I want to stop only while searching the data –  May 05 '15 at 08:49
  • 1
    Are you talking about your first ajax call now? If yes, just add the `setInterval(sTimeOut, interval)` line into the complete callback of your second ajax call. – kidA May 05 '15 at 08:53
  • @kidA thanku for your reply:) –  May 05 '15 at 08:56

1 Answers1

0

You can abort the request.

You can declare a object to keep the current executing request, and to check if searching is running. If the search is running starting another reuquest will pe skiped.

var request = {
    xhr: {},
    isSearching: false
};

var sTimeOut = function () {
    if(!request.isSearching) // prevent to run other request when is searching
    {
        request.xhr = $.ajax(
        {
            type: "POST",
            url: '@Url.Action("checkJobIDs")',
            dataType: "json",
            async: true,
            success: function (data) {

            }
        });
    }
}

  var interval = 10000;
  setInterval(sTimeOut, interval);

When is searching the request is aborted and prevent to execute anothers. After the search is completed you can resume the request calling in background.

$.ajax(
{
    type: "POST",
    url: '@Url.Action("searchData")',
    dataType: "json",
    mtype: "post",
    data: { str: str },
    async: true,
    beforeSend: function() { // stop background process
        request.isSearching = true;
        request.xhr.abort()
    },
    complete: function () {  // resume background process
        request.isSearching = false;
    },
    success: function (data) {
        search(data.searched[0]);
    }
});
Community
  • 1
  • 1
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • after search completion will background ajax will start again? –  May 05 '15 at 08:54
  • Yes, by setting `request.isSearching = false;` if you don't want to ever start again, you can use `clearInterval ` or to keep `request.IsSearching = true;` if you need to resume it again. – adricadar May 05 '15 at 08:55