带有jQuery.html的setInterval仅更新一次? [英] setInterval with jQuery.html only updates once?

查看:110
本文介绍了带有jQuery.html的setInterval仅更新一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这将在3秒后用随机数更新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.

推荐答案

Ha. PHP在SERVER端运行.客户端上有JS.

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

您需要在JS端而不是php端生成rand ...

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

js代码:

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

这篇关于带有jQuery.html的setInterval仅更新一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆