随随时间变化的随机值进行除法 [英] Div with random values that change with time

查看:51
本文介绍了随随时间变化的随机值进行除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在HTML页面中创建一个div并在其中写入随机值,然后,我希望div每X秒刷新一次自身,以使其中的数字发生变化,但不会重新加载整个页面,只是div.

I'm trying to create a div in an HTML page and write random value in it, then, I want the div to refresh itself every X seconds so that the number in it change, but the entire page is not reloaded, just the div.

我的想法是:

<body>
 <div id="people" onload="rand();"> </div>

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

   <script>
     function autoRefresh_div()
     {
      $("#people").load("load.html");// a function which will load data from other file after x seconds
     }

     setInterval('autoRefresh_div()', 0.5); // refresh div after 5 secs

    function rand(){
     document.getElementById("people").innerHTML = Math.random();
}

  </script>
</body>
</html>

但是我的div中没有​​任何内容.我尝试用一​​些文本更改"Math.random()",但没有任何变化.

But nothing appear in my div. I tried to change "Math.random()" with some text but nothings change.

有人可以解释为什么吗?

Can someone explain me why?

推荐答案

您不需要onload事件. setInterval()带有将在div中设置新值的函数:

You don't need an onload event. Just setInterval() with a function which will set a new value in a div:

function autoRefreshDiv() {
  document.getElementById("people").innerHTML = Math.random();
}
setInterval(autoRefreshDiv, 1000);  // Time is set in milliseconds

<div id="people"></div>

setInterval将每X毫秒运行一次函数.

setInterval will run the function every X milliseconds.

这篇关于随随时间变化的随机值进行除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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