3 回答
TA贡献646条经验 获得超225个赞
简单改造了下:
<script type="text/javascript"> function clock(){ var time=new Date(); document.getElementById("c").value = time; } var i; function start(){ i = setInterval(clock,100); } function stop(){ clearInterval(i); } start() </script> </head> <body> <form> <input type="text" id="c" size="50" /> <input type="button" value="Stop" onclick="stop()" /> <input type="button" value="Start" onclick="start()" /> </form> </body>
TA贡献55条经验 获得超11个赞
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
<input type="button" value="Stop" onclick="clearInterval(i)" />
<input type="button" value="Start" onclick="clock()" />
</form>
</body>
<script type="text/javascript">
function clock(){
var time=new Date();
document.getElementById("clock").value = time;
}
var i=setInterval(clock,100);
</script>
直接看代码吧,js要放到下面,这样才能取到元素
TA贡献3593条经验 获得超0个赞
TA贡献53条经验 获得超11个赞
因为clock()函数和input的id命名冲突了,所以不行,要想实现那个功能
function clock(){
var time=new Date();
document.getElementById("clock").value = time;
i = setTimeout(clock,100);
}
var i;
<input type="button" value="Stop" onclick="clearTimeout(i)" />
其他不变
添加回答
举报