想实现的功能如下,
                 页面上有“显示时间”和“时间静止”两个按钮。
                 单击“显示时间”,状态栏动态的显示时间。单击“时间静止”,状态栏的时间不再变化。
下面的代码问题是,单击“时间静止“时,状态栏时间不会停,这个该怎么改?<html>
<script type="text/javascript">var tmp
function show()
{  
   var d= new Date();
   var k=d.getHours()+ ":" +d.getMinutes()+":" +d.getSeconds()
   window.status=k;
   
   tmp=setInterval("show()",1000)

}function stop()
{
    clearInterval(tmp);
}
</script><body ><input type="button" id="t1" value="显示时间" onclick="show()">
<input type="button" id="t2" value="时间静止" onclick="stop()"></body></html>

解决方案 »

  1.   

    呵  只要把tmp=setInterval("show()",1000)拿到函数以外的脚本标签里定义就行了
      

  2.   

    <!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=utf-8" />
    <title>无标题文档</title></head><script type="text/javascript">var tmp = -1;function show()
    {  
      var d= new Date();
      var k=d.getHours()+ ":" +d.getMinutes()+":" +d.getSeconds()
      window.status=k;
       
      tmp = setTimeout("show()",1000)}function stop()
    {
      clearTimeout(tmp);
    }
    </script><body ><input type="button" id="t1" value="显示时间" onclick="show()">
    <input type="button" id="t2" value="时间静止" onclick="stop()"></body>
    </html>
      

  3.   

    setInterval 是不断的的执行,那样很难停止的
      

  4.   

      tmp=setInterval("show()",1000) 改成 tmp=setTimeout("show()",1000);或是
      tmp=setInterval("show()",1000)  写到show函数外面
      

  5.   


    function show()
    {   
      var d= new Date();
      var k=d.getHours()+ ":" +d.getMinutes()+":" +d.getSeconds()
      window.status=k;
        
      tmp=setInterval("show()",1000)}..你这个是死循环啊..每次调用show.都重新定义了一个setInterval..
    把setInterval放到函数show外面去