<script language="javascript" type="text/javascript">
self.setTimeout("showTime()",5000);
function showTime(){
var date = new Date();
document.getElementById("clock").value = date;
self.setInterval("showTime()",5);
}
</script>
<button id="clock" style="width:300px; border:none;" onclick="window.clearInterval(int);"></button>

解决方案 »

  1.   

    js运行占用了大量的内存
     self.setInterval("showTime()",5); 重复执行的间隔太小了吧
      

  2.   

    setTimeout
    等于新开一个延迟任务.
    setInterval
    等于新开一个定时任务.延迟任务只是对自身的延迟.然后执行定义中的方法.
    定时任务是不断增加地调用定义中的方法.你这个用法等于1秒产生2^200个showTime方法实例.能不占内存吗?
      

  3.   

    这样处理一下:
    <button id="clock" style="width:300px; border:none;" onclick="window.clearTimeout(timer);">Time</button><script type="text/javascript">
    var dd = document.getElementById("clock");//将DOM对象取出,减少每次查找DOM的时间
    var timer = window.setTimeout(showTime,1000);
    function showTime(){
        dd.innerHTML = new Date();
        timer = window.setTimeout(showTime,5);//全部改用timeouts方式
    }
    </script>
      

  4.   

    3楼正解另外 把window.setTimeout(showTime,5);
    改成 window.setTimeout(showTime,500);或window.setTimeout(showTime,1000);
    1000=1秒
      

  5.   

    self.setInterval("showTime()",5);
    你的问题在这,每过5毫秒就会有一个新的setInterval()被添加,并且关闭前无限添加