日期:2014-05-16  浏览次数:20432 次

关于window.setInterval()
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function hello()
{
document.write(Math.random());
//alert("这是延迟后显示出来的!");
}
window.setInterval("hello()",2000);
</script>
</body>
</html>



以上代码为什么无法在每2秒后执行document.write(Math.random());的效果
但是如果如下写
function hello()
{
//document.write(Math.random());
alert("这是延迟后显示出来的!");
}

则可以达到每2秒后执行alert("这是延迟后显示出来的!");
的效果

请问高手什么原因??
HTML 函数

------解决方案--------------------
引用:
那也不对啊,我的代码:
<body>
<script type="text/javascript">
function hello()
{
document.write(Math.random());
//alert("这是延迟后显示出来的!");
}
window.setInterval("hello()",2000);
</script>
</bo……

楼主,document的内容被清空了,js代码也没了,整个document 里,只有hello()第一次输出的随机数,js没了,interval也没了,代码没机会执行第二次!