python小菜 2012-08-13 19:39 采纳率: 0%
浏览 132

JQuery AJAX轮询语法

I'm a bit green, so please bear with me.

I need to poll a SharePoint webservice until it returns a value. I believe that I have formatted my code incorrectly. If there is a pre-existing thread that addresses this, please point me to it; my relatively limited understanding may have kept me from recognizing it.

function Poll2(){
    $.ajax({
        //Use an ABSOLUTE reference to your target webservice
        url: "https://mydomain.com/Sandbox/bitest/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        success:processResult,
        complete: Poll2,
        timeout: 5000,
        contentType: "text/xml; charset=\"utf-8\"",
        async: true
    });
}

My desire is that it would poll the service every 5 seconds until success, at which point it would proceed to the "processResult" function where all the data will be handled. I am afraid that I have created an infinite loop by referencing the parent function.

-------------------EDIT & NEW CODE-------------------

I have found a solution in this blog post that roughly accomplishes what I am looking for. As it turns out, I really only want my request to fire once. However, in regards to an "infinite" polling routine, this does the job quite nicely.

(function poll() {
    setTimeout(function () {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: 'http://somewhere.com/rest/123',
            success: function (data) {
                MyNamespace.myFunction(data); //DO ANY PROCESS HERE
            },
            complete: poll
        });
    }, 5000);
})();

I would note, however, that this routine does not initialize its first poll until 5 seconds after it is executed. Simple piece of code though! Thanks much to the author.

  • 写回答

3条回答 默认 最新

  • local-host 2012-08-13 19:42
    关注

    You are surely right, to have created an infinite loop.

    But you could just check, if the request has to be made again or not and if not, just process the data (if needed here) and return from the Poll2 function.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。