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

javascript关于变量作参数的问题
代码如下:
<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函数响应可以。这是为什么?
是变量生命周期的问题么?求教

------解决方案--------------------
经测试。你把id换个名字就好了。比如ids