重复AJAX调用之间延迟到Web方法 [英] Delay between repeated AJAX call to a web method

查看:131
本文介绍了重复AJAX调用之间延迟到Web方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用jQuery在客户端实现的ASP.Net Web应用程序。在while循环中,客户端的jQuery脚本使得在服务器端code Web方法的异步调用。调用返回其jQuery使用更新用户长时间运行的服务器端任务的状态。我们的目标是让jQuery的重复调用服务器,直到状态完成,一旦完成它打破了while循环,并通知thatthe任务就完成了该用户。

我的问题是,低于code在循环运行,但我想让它以每次调用之间的延迟或睡眠prevent铺天盖地的服务器状态请求。我试图在$ C $低于C调用setTimeout的,但它仅适用于最初的Ajax调用,出现背靠背每个后续调用。有没有一种方法能够有效地推迟以后每次调用服务器?这是实现我所描述的那种行为的最有效的方法是什么?理想情况下,我想每个调用之间的2-5秒的延迟。

我有以下的code

客户端jQuery的:

  VAR alertTimerId;
$('输入[名称= btnStatus])。点击(函数(){

    VAR的结果= FALSE;
    //循环而服务器报告任务完成是真实的
    而(!结果){
      alertTimerId = setTimeout的(功能(){
        $阿贾克斯({
          键入:POST,
          网址:Default.aspx的/的getStatus
          数据: {},
          的contentType:应用/ JSON的;字符集= UTF-8,
          数据类型:JSON,
          异步:真正的,
          成功:函数(MSG){
            如果(msg.d!= FALSE)
              结果= msg.d;
          },
          错误:函数(XHR,ajaxOptions,thrownError){
            警报(AJAX失败);
          }
        });
      },2000);


      如果(计数大于0){
        计数 - ;
      }
    }


  });
 

服务器端ASP

  [System.Web.Services.WebMethod]
        公共静态布尔的getStatus()
        {
            返回结果;
        }
 

解决方案

如何像下面这样?这个想法是,Ajax调用封装在一个函数, doAjax(),然后从阿贾克斯成功处理程序中,如果结果是假的,你使用的setTimeout()来排队再次调用 doAjax ,否则你采取任何行动要采取真正的结果。 (你可以有选择的呼叫 doAjax()从阿贾克斯错误处理程序了。)

  $('输入[名称= btnStatus])。点击(函数(){

    传播doAjax(){
       $阿贾克斯({
          键入:POST,
          网址:Default.aspx的/的getStatus
          数据: {},
          的contentType:应用/ JSON的;字符集= UTF-8,
          数据类型:JSON,
          异步:真正的,
          成功:函数(MSG){
             如果(!msg.d)
                 的setTimeout(doAjax,2000年);
             其他 {
                 // 成功!在这里通知用户
             }
          },
          错误:函数(XHR,ajaxOptions,thrownError){
            警报(AJAX失败);
          }
       });
    }

    doAjax();

});
 

(注:我已经删除了if语句与计数,因为它似乎没有任何的问题的相关性。如果你真正的code使用它而已。阿贾克斯成功处理程序内更新过。)

I have a ASP.Net web app with jquery implemented on the client side. within a while loop, the client side jquery script makes an asynchronous call to a web method in the server side code. The call returns the status of a long running server side task which jquery uses to update the user. The goal is to have jquery repeatedly call the server until the status is complete, once done it breaks out of the while loop and notifies the user thatthe task is complete.

My problem is that the below code runs in a while loop, but I want to make it delay or sleep between each call in order to prevent overwhelming the server with status requests. I tried calling setTimeout in the code below, but it only works with the initial ajax call, every subsequent call occurs back to back. Is there a way to efficiently delay each subsequent call to the server? Is this the most efficient way to achieve the kind of behavior I'm describing? Ideally I'd like a 2-5 second delay between each call.

I have the following code

Client Jquery:

var alertTimerId;
$('input[name=btnStatus]').click(function () {

    var result  = false;
    //Loop while server reports task complete is true
    while (!result) {
      alertTimerId = setTimeout(function () {
        $.ajax({
          type: "POST",
          url: "Default.aspx/GetStatus",
          data: "{}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          async: true,
          success: function (msg) {
            if(msg.d != false)
              result = msg.d;
          },
          error: function (xhr, ajaxOptions, thrownError) {
            alert('AJAX failure');
          }
        });
      }, 2000);


      if (count > 0) {
        count--;
      }
    }


  });

Server Side ASP

[System.Web.Services.WebMethod]
        public static bool GetStatus()
        {
            return result;
        }

解决方案

How about something like the following? The idea is that the Ajax call is encapsulated in a function, doAjax(), and then from within the Ajax success handler if the result is false you use setTimeout() to queue up another call to doAjax, otherwise you take whatever action you want to take for a true result. (You could optionally call doAjax() from the Ajax error handler too.)

$('input[name=btnStatus]').click(function () {

    function doAjax() {
       $.ajax({
          type: "POST",
          url: "Default.aspx/GetStatus",
          data: "{}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          async: true,
          success: function (msg) {
             if (!msg.d)
                 setTimeout(doAjax, 2000);
             else {
                 // Success! Notify user here
             }
          },
          error: function (xhr, ajaxOptions, thrownError) {
            alert('AJAX failure');
          }
       });
    }

    doAjax();

});

(Note: I've removed the if statement with count, since it seemed to have no relevance to the question. If your real code uses it just update it within the Ajax success handler too.)

这篇关于重复AJAX调用之间延迟到Web方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆