我想调用网络服务,直到获得所需的输出。 这是我正在使用的代码。
$.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 秒执行一次。
最佳答案
这是一个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
}
});
}
关于javascript - 如何在成功时继续调用 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493726/