setInterval()仅在页面加载时更新

时间:2019-05-29 16:16:51

标签: javascript php html mysql setinterval

我正在尝试添加一个进度条,该进度条每两分钟为用户提供10分。我的代码添加了点并更新了进度条,但是在页面加载后仅更新了一次。我需要帮助让setInterval()正常工作。

我尝试使用setTimeout()递归设置它,并遇到相同的问题。我的确在setInterval中调用了该函数中的一些php代码,该代码可以正常工作并将点添加到用户点和totalPoints中,但可能会弄乱setInterval()?

<script src="script.js" type="text/javascript">
    window.onload = function () {
      setPoints();
      playing();
    };

    function addPoints() {
       if (progress >= total.innerHTML) {
         clearInterval(id);
         //total.innerHTML = total.innerHTML * 2;
         elem.style.width = 0;
         pnts.innerHTML = 0;
         progress = pnts.innerHTML;
     } else {
         <?php
    $u1 = "UPDATE users SET points = points + 10 WHERE username = '$username'";
    mysqli_query($link, $u1) or die('Error querying database.');
    $u2 = "UPDATE users SET totalPoints = totalPoints + 10 WHERE username = '$username'";
    mysqli_query($link, $u2) or die('Error querying database.');
    $points += 10;
    $totalPoints += 10;
  ?>
         points.innerHTML = <?php echo $points;?>;
         pnts.innerHTML = <?php if($total == 100){
        echo $totalPoints;
        }else{echo ($totalPoints - ($total/2));}?> * 1;
            //progress = pnts.innerHTML;
            elem.style.width = (progress/total.innerHTML) * 100 + '%';
         }
     }

     var addTen = addPoints();
     if(playing()) {
       var id = setInterval(addTen, 5000);
     }
     </script>

我希望它每5秒钟更新一次atm,当页面加载或刷新时它只会更新一次。

0 个答案:

没有答案