AJAX调用使用了我所有的web主机的内存和I/O

AJAX calls are using all my webhost's memory and I/O

本文关键字:主机 web 内存 调用 AJAX      更新时间:2023-09-26

我正在尝试运行一些正在敲打我的虚拟主机的内存和I/O使用的代码。我不确定如何清理或修复这些代码,因为这是我第一次尝试使用AJAX技术。

JS只需要返回它每20秒调用的PHP脚本的值,并将其显示在一个空的<div>标记中。我是AJAX的新手,我不确定我的问题在哪里以及如何解决它。

这是我的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>

我的PHP脚本叫做:

// 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;

你在你的成功回调中创建了一个间隔,所以每次你的成功函数运行时,你都在创建一个新的间隔,而不是你已经拥有的那个,以此类推,所以你最终得到了无限的间隔。

尝试在ajax调用之外创建间隔

      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);

尝试将"setInterval(update, 20000);"从success移开。这应该有帮助。