代码如下:
<html>
<head>
<script language="javascript">
var id;
function GetTime()
{
var time = new Date();
form1.text1.value = time.toLocaleString();
}
function ShowTime()
{
id = setInterval("GetTime()",1000);
}
// function Cancel()
// {
// clearInterval(id);
// }
</script>
</head>
<body onLoad="ShowTime()">
<form name="form1" method="post" action="">
当前时间为:<input type="text" name="text1" style="width:300px;height:20px" />
<input type="button" name="cancel" value="暂停" onclick="window.clearInterval(id);" />
</form>
</body>
</html>这里onclick用<head>标签里的变量id作为参数传入好像没达到预期效果。用注释掉的cancle函数响应可以。这是为什么?
是变量生命周期的问题么?求教

解决方案 »

  1.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
    <head>
    <script language="javascript">
        var id;
        function GetTime() {
            var time = new Date();
            form1.text1.value = time.toLocaleString();
        }
        function ShowTime() {
            id = setInterval("GetTime()", 1000);
        }
       function Cancel()
         {
         clearInterval(id);
       }
    </script>
    </head>
    <body onLoad="ShowTime()">
    <form name="form1" method="post" action="">
    当前时间为:<input type="text" name="text1" style="width:300px;height:20px" />
    <input type="button" name="cancel" value="暂停" onclick="Cancel()" />
    </form>
    </body>
    </html>
    你那是什么传参啊
      

  2.   

    经测试。你把id换个名字就好了。比如ids