如何继续在成功上调用Jquery

时间:2013-03-19 07:27:33

标签: javascript jquery

我想调用webservice,直到获得所需的输出。 这是我正在使用的代码。

$.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);
                }
});

此网络服务将返回结果。我希望在结果完成之前,jquery调用应该每1秒执行一次。

5 个答案:

答案 0 :(得分:1)

试试这个:

function ajaxCall() {
    $.ajax({
        ...
        success: function (response) {
            //got desired output?
            /*logic*/
            //no?
            setTimeOut(ajaxCall, 1000);
        },
        error: function() {
            setTimeOut(ajaxCall, 1000);
        }
    });
}

答案 1 :(得分:1)

这是good start

我们的想法是将您的代码封装到方法中,然后使用setInterval调用它:

setInterval(ajaxCall, 1000);

例如:

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
                  }
  });
}

答案 2 :(得分:1)

使用jQuery的Deferred / promise机制的变体:

// 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();
}

用法:

poll(
    "../WebService/myservice.asmx/GetStatus",
    {myparameter: value}, 1000, 100
)
.then(function (response) {
    // ...
})
.fail(function (error) {
    alert(error);
    // ...
})

答案 3 :(得分:0)

将ajax-request包裹在命名函数中,只要需要发出其他请求,就可以调用该函数。在成功回调中,您检查结果,如果您对此不满意,请设置超时以在一秒钟内再次调用该函数。

这样的事情:

(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);
        }
   });
})();

答案 4 :(得分:0)

正如之前的评论所解释的那样,如果你想让它每隔一秒(或一段时间)打电话。您可以使用setTimeout()(函数内部)或setInterval()(外部)。但是如果你想继续尝试直到你得到所需的代码,你最好在函数内部使用循环:

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);
                        }
        });
    }
}