菜单切换
FACESOHO知行者
心灵
记录
远方
赞赏工具
源代码:
点击运行
保存
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>小鸟启蒙(facesoho.com)</title> </head> <body> <button onclick="startCount()">开始计数!</button> <input type="text" id="txt"> <button onclick="stopCount()">停止计数!</button> <p> 点击 "开始计数!" 按钮开始执行计数程序。输入框从 0 开始计算。 点击 "停止计数!" 按钮停止后,可以再次点击点击 "开始计数!" 按钮会重新开始计数。 </p> <script> var c = 0; var t; var timer_is_on = 0; function timedCount() { document.getElementById("txt").value = c; c = c + 1; t = setTimeout(function(){ timedCount() }, 1000); } function startCount() { if (!timer_is_on) { timer_is_on = 1; timedCount(); } } function stopCount() { clearTimeout(t); timer_is_on = 0; } </script> </body> </html>
运行结果