<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type = "text" id="clock" style="width: 449px" value="点击右侧[begin]按钮开始"> <input type = button value="begin" onclick="begin_click()"> <input type = button value="End" onclick="End()"> <script> function begin() { var stime = Date(); var ret = document.getElementById("clock"); ret.value = stime; } var ID; function begin_click() { begin() clearInterval(ID) ID = setInterval(begin,1000) } function End() { clearInterval(ID); } </script> </body> </html>
知识点1:begin_click 里面放个 begin 即时刷新出来当前事件。
知识点2:begin_click 里面放个 clearInterval(ID) 即时结束上个id。