dpvv37755 2016-10-18 20:58
浏览 33
已采纳

AJAX调用正在使用我的所有webhost的内存和I / O.

I am attempting to run some code that is hammering my web host's memory and I/O usage. I am unsure how I can clean up or fix this code, as this is the first time I've tried using the AJAX technology.

The JS simply needs to return the value of the PHP script it is calling every 20 seconds and display it in an empty <div> tag. I am very new with AJAX and I am not sure where my problem is and how to fix it.

Here is my Javascript:

<script type="text/javascript">
  $(document).ready(function() {

  function update() {
    $.ajax({
      type: 'POST',
      url: 'time_hash.php',
      dataType: 'text',
      timeout: 20000,
      success: function(data) {
        $(".dispKey").html(data); 
        setInterval(update, 20000);
     }
   });// End ajax call
 }// End function update()
 update();

});
</script>

And my PHP script being called:

// Modify the server time to round down to the nearest 20 seconds
$modTime = (time() - (time() % 20));

// Create one-way hash from modified time
$hashToken = md5($modTime);

// Truncate hash to the first 6 characters
$dispToken = strtoupper(substr($hashToken, 0, 6));

// Display the 'generated key' -- uppercase.
echo $dispToken;

Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • douhan9748 2016-10-18 21:03
    关注

    You are creating the interval inside your success callback, so every time your success function runs you are creating a new interval other than the one you already had and so on, so you end up with infinite intervals.

    try creating the interval outside your ajax call

          function update() {
            $.ajax({
              type: 'POST',
              url: 'time_hash.php',
              dataType: 'text',
              timeout: 20000,
              success: function(data) {
                $(".dispKey").html(data); 
             }
           });// End ajax call
         }// End function update()
          setInterval(update, 20000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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之后自动重连失效