weixin_33704591 2016-07-23 22:25 采纳率: 0%
浏览 334

在while循环中延迟

So I want to do a deferred ajax request in jquery until I receive a specific server response (non-null). How would I go about doing that?

while (data.response != null) {
  $.ajax(..).done(function(data);
}

function doUntilResult() {
    Server.doA().done(function(data) {
      Server.doB(data).done(function(result) {
          //if result == null, repeat again
      });
    });
}
  • 写回答

2条回答 默认 最新

  • weixin_33698043 2016-07-23 22:49
    关注

    You can't loop like this in Javascript. Javascript is an event-driven system. When you are looping like this, no other events can ever get processed. As such, not even your first Ajax call will get processed. In fact, you will probably just drive the JS engine into the ground by running out of some resource as the client tries to make millions of ajax calls all at once.

    Instead, you can use the ajax result asynchronously and then decide from the result whether to call it again:

    function poll() {
        $.ajax(...).then(function(data) {
            if (!data.xxxx) {
                // call it again after some short delay
                setTimeout(poll, 1000);
            } else {
                // got the result we wanted, process it
            }
        })
    }
    
    // start the polling
    poll();
    

    If you want the whole thing to return a promise, you can do this:

    function delay(t) {
        return new $.Deferred(function(def) {
            setTimeout(def.resolve, t);
        }).promise();
    }
    
    function poll() {
        return $.ajax(...).then(function(data) {
            if (!data.xxxx) {
                // call it again after some short delay
                return delay(1000).then(poll);
            } else {
                // got the result we wanted, process it
                return someValue;
            }
        })
    }
    
    // start the polling
    poll().then(function(result) {
        // process result here
    });
    

    P.S. It is pretty much never the right thing to just constantly poll some server as fast as you possibly can. This might work fine if you only have a couple of users, but as soon as you have lots of users, all that will do is overwhelm your server with empty polling requests where most of the time, there's nothing to return to the client. This is nightmare for handling load. It's even better to find an architecture that is not polling, but if you are going to poll, then at least use some sort of timer to poll at some future time rather than as fast as possible.

    P.P.S. If you're really just waiting for some value to change on the server, then perhaps you should use a webSocket connection. With that, the client establishes the connection to the server and the connection persists and then at any point in the future, the server can simply send a message to the client. The client doesn't have to poll at all. This can be massively more efficient on your server infrastructure and can deliver more timely results.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效