如何在不重新加载页面的情况下刷新div和mysql代码

时间:2016-07-06 04:30:52

标签: php jquery mysql css3

嘿,我对此有点新意,但我目前正在编写一个小游戏,我有一个使用jquery,css和php的健康栏。我使用mysql查询从数据库获取健康状况的值,它只能在我重新加载页面时工作。我希望健康栏的值能够不断更新我想要的每秒x。



< script >
  function UpdateStats() {
    $.getJSON('demo1.php', function(stats) {
      $('#pbar_innerdiv').animate({
        width: (stats.health) + '%'
      }, 200);
    });

  }, 1000; < /script>
&#13;
#pbar_outerdiv {
  margin-top: 5px;
  margin-left: 90px;
}
#pbar_innertext {
  margin-top: 6px;
  margin-left: 7px;
}
&#13;
<div id="pbar_outerdiv" style="width: 580px; height: 32px; border: 1px solid white; z-index: 1; position: relative; border-radius: 2px; -moz-border-radius: 5px;">
  <div id="pbar_innerdiv" style="background-color: maroon; z-index: 2; height: 100%; width: <?php echo $health;?>%;"></div>
  <div id="pbar_innertext" style="z-index: 3; position: absolute; top: 0; left: 0; width: 100%; height: 100%; color: white; font-weight: bold; text-align: center;">
    <?php echo $health;?>/100</div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

像这样改变你的剧本

DIV

这个比setinterval更好,它会在第一次完成时调用第二次迭代。希望它会有所帮助

< script > function UpdateStats() { $.getJSON('demo1.php', function(stats) { $('#pbar_innerdiv').animate({ width: (stats.health) + '%' }, 200); }); setTimeout(function(){UpdateStats()}, 3000); } UpdateStats(); < /script> 首先它会在UpdateStats()完成后调用UpdateStats()函数然后等待3秒然后它将调用第二次迭代。这个将重复完成