setInterval with jQuery.html only updates once?

随声附和 提交于 2019-12-24 10:40:01

问题


<?php
  echo '
    <script type="text/javascript">
      $(function() {
        var refresh = setInterval(function() {
          $("#content").html("'.rand().'");
        }, 3000);
      });
    </script>

    <div id="content"></div>
  ';
?>

This will update the div "content" with a random number after 3 seconds, however it only updates once. Why does it not continually generate a new number every three seconds, and how can I make it do precisely that?

Thank you in advance for any assistance rendered.


回答1:


Ha. PHP is run on the SERVER side. JS is on the client.

you need to generate the rand on the JS side not he php side...

js code:

<script type="text/javascript">
  $(function() {
    var refresh = setInterval(function() {
      var randomnumber = Math.floor(Math.random()*11);
      //where 11 dictates that the random number will fall between 0-10
      $("#content").html(randomnumber);
    }, 3000);
  });
</script>

<div id="content"></div>


来源:https://stackoverflow.com/questions/5412591/setinterval-with-jquery-html-only-updates-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!