javascript - 如何在成功时继续调用 Jquery

标签 javascript jquery

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

$.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/

相关文章:

javascript - jQuery .text() 显示双文本

javascript - jQuery 仅适用于浏览器首次启动

javascript - 将日期时间从服务器转换为字符串javascript

jquery - 您可以在不更改 html 的情况下覆盖白色和黑色的 Jquery 移动股票图标吗?

没有任何 php 代码的 Javascript 文件

javascript - Owl Carousel : change slide on hover dots

javascript - useState 根本不更新数组

javascript - 根据空格拆分字符串并在angular2中读取

jquery - 悬停时图像放大,div 框中的内容附加到图像

Jquery:如果 url 包含特定哈希值,则有条件地添加类