weixin_33670786 2018-06-05 14:02 采纳率: 0%
浏览 20

Ajax中的计时器-抢占[重复]

This question already has an answer here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/50705802/ajax-too-slow-recursion" dir="ltr">Ajax too slow - Recursion</a>
                            <span class="question-originals-answer-count">
                                (1 answer)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-06-16 11:16:18Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

So this thing was going in my mind for a long time whether the timer that is given in an AJAx after which it has to send another request, what if it is smaller than the actual time taken by the requested file to complete its operation.

for example, consider the below code,

<div class="item"></div>
<script>
function timeLeft() {
  $(".item").each(function() {
    $this = $(this);
    var dataString = {s: "//some data", st: "<?echo $stamp?>"};
    $.ajaxSetup({cache:false});
    $.ajax({
      type: "POST",
      url: "get_content_home.php",
      dataType: "html",
      data: dataString, 
      success: function(result) {
        $this.html(result);
      }
    });
  });
}
window.setInterval(function() {
  timeLeft();
}, 100);
</script>

the timer given here is 100ms and the file get_content_home.php will be requested every 100m. What if get_content_home.php takes 500ms to complete its operations. Will the get_content_home.php be preempted and will be requested again? or will the timer wait and delay itself.

Thanks in advance.

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33724659 2018-06-05 14:14
    关注

    It's worse than you thought since the ajax request is in a loop.

    What's gonna happen is actually :

    1. window.setInterval call timeLeft

    2. timeLeft call the AJAX request to get_content_home.phpone time for each .item element.

    3. Let's say one AJAX call take 500ms, you'll do this five time before the first AJAX request return something (hence 5xnumber of .item calls before one result).

    To stop this craziness, put your AJAX call outside of the loop and put the window.setInterval in the AJAX callback function:

    $.ajax({
      type: "POST",
      url: "get_content_home.php",
      dataType: "html",
      data: dataString, 
      success: function(result) {
        $this.html(result);
        window.setTimeOut(function() {
          timeLeft();
        }, 100);
      }
    });
    

    and call timeLeft(); just once at the start.

    评论

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中