test.js代码如下:
var i = 0;
function fun()
{
 document.write(i);
 document.write("\n");
 i++;
}
function JavaTime()
{
setInterval('fun()',2000);
}
test.php代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script src="test.js" type="text/javascript"></script>
<?phpecho "<script language='JavaScript' type='text/javascript'>JavaTime();</script>"?>
<body>
</body>
</html>为什么js定时器只执行一次,然后浏览器一直处于这个状态:
只显示0,然后浏览器一直在转

解决方案 »

  1.   

    我也不怎么懂JS。换成alert(i)数字就会变化,奇怪了。
      

  2.   

    楼主,你换个不是IE内核的浏览器试试,比如chrome内核的,可以得到你想要的效果,但是换行不是\n而是<br />
      

  3.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">function JavaTime()
    {
        var icolor=0;
        var iNum=256;
        setTimeout(fun, 1000);
        function fun()
        {
                document.write(icolor);
                document.write('<br/>');
                icolor++;
                setTimeout(fun, 1000);
                    
        }
    }
    </script>
    </head>
    <body>
    <?php
    echo "<script language='JavaScript' type='text/javascript'>JavaTime();</script>";
    ?>
    </body>
    </html>你这么写看看。我测过没有问题。
      

  4.   

    楼上说行的,都是在胡说!
    当页面加载完成时,页面的载体 document 将被关闭
    之后的 document.write 将在新的 document 中写
    尽管非 ie 系列的浏览器只将 body 节点作为 document 的根,表面上 js 依然被执行了,但是 body 中原来的内容都不复存在了因此只要是页面加载完成后才执行的 js,都只能用 innerHTML、innerText 操作页面内容或以 alert 显示信息