2

I want to call the webservice until i get the desired Output. This is the code i am using.

$.ajax(
{
    type: "POST",
        url: "../WebService/myservice.asmx/GetStatus",
        data: "{'myparameter': '" + value + "'}",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (response) {
                 return response.d;
                 },
        error: function (x, e) {
                alert("The call to the server side failed. " + x.responseText);
                }
});

This webservice will return me the result. i want that until the result is completed the jquery call should execute every 1 sec.

Moiz
  • 2,409
  • 5
  • 27
  • 50

5 Answers5

1

Try this:

function ajaxCall() {
    $.ajax({
        ...
        success: function (response) {
            //got desired output?
            /*logic*/
            //no?
            setTimeOut(ajaxCall, 1000);
        },
        error: function() {
            setTimeOut(ajaxCall, 1000);
        }
    });
}
Sergio
  • 6,900
  • 5
  • 31
  • 55
1

Here is a good start

The idea is to encapsulate your code into a method and then call it using setInterval:

setInterval(ajaxCall, 1000);

For example:

function ajaxCall(){
  $.ajax(
  {
      type: "POST",
          url: "../WebService/myservice.asmx/GetStatus",
          data: "{'myparameter': '" + value + "'}",
          contentType: "application/json;charset=utf-8",
          dataType: "json",
          async: true,
          cache: false,
          success: function (response) {
                   return response.d;
                   setInterval(ajaxCall, 1000); //call itself
                   },
          error: function (x, e) {
                  alert("The call to the server side failed. " + x.responseText);
                  setInterval(ajaxCall, 1000); //try again
                  }
  });
}
Community
  • 1
  • 1
Ulises
  • 13,229
  • 5
  • 34
  • 50
1

A variant using jQuery's Deferred/promise mechanism:

// interval in ms, use retries = 0 for unlimited retries
function poll(url, data, interval, retries) {
    var result = $.Deferred(), retryCount = 0;

    (function repeat() {
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            contentType: "application/json; Charset=utf-8",
            dataType: "json",
            async: true,
            cache: false
        })
        .then(function (response) {
            if (response.d === "success") {
                result.resolve(response);
            }
        })
        .fail(function (x, e) {
            if (retries === ++retryCount) {
                result.reject("Maximum number of retries reached (" + url + ")");
            } else {
                setTimeout(repeat, interval);
            }
        });
    })();

    return result.promise();
}

Usage:

poll(
    "../WebService/myservice.asmx/GetStatus",
    {myparameter: value}, 1000, 100
)
.then(function (response) {
    // ...
})
.fail(function (error) {
    alert(error);
    // ...
})
Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

Wrap your ajax-request in a named function, that you can call whenever you need to make another request. In the success-callback you check the result, and if you aren't happy with it, set a timeout to call the function again in one second.

Something like this:

(function Fetch (){
   $.ajax({
        type: "POST",
        url: "../WebService/myservice.asmx/GetStatus",
        data: "{'myparameter': '" + value + "'}",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (response) {
            if (response != "completed") {
                setTimeout(Fetch, 1000);
            } else {
                // Do something when completed 
            }
        },
        error: function (x, e) {
            alert("The call to the server side failed. " + x.responseText);
        }
   });
})();
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
0

As the earlier comments explained, if you wanted it to call every second (or period of time). you can use either setTimeout()(inside the function) or setInterval()(outside). But if you wanted to keep trying just until you got the desired code, you better use looping inside the function:

function startRequest() {
    while(1){
        $.ajax(
        {
            type: "POST",
                url: "../WebService/myservice.asmx/GetStatus",
                data: "{'myparameter': '" + value + "'}",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success: function (response) {
                         return response.d;
                         i=0;
                         break;
                         },
                error: function (x, e) {
                        alert("The call to the server side failed. " + x.responseText);
                        }
        });
    }
}
aIKid
  • 26,968
  • 4
  • 39
  • 65