Home > Web Front-end > JS Tutorial > body text

Design a simple stopwatch timer with javascript_javascript skills

WBOY
Release: 2016-05-16 15:31:14
Original
1079 people have browsed it

The example in this article describes the implementation code of a simple stopwatch timer designed in JavaScript. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:

The specific code is as follows:

<html> 
<head> 
<title> New Document </title> 
</head> 
<body> 
 <form action="somepage.asp"> 
  <input type="text" value="0" name="txt1"/> 
  <input type="button" value="开始" name="btnStart"/> 
  <input type="button" value="重置" name="btnReset"/> 
 </form> 
</body> 
</html> 
<script language="JavaScript" type="text/javascript"> 
<!-- 
//获取表单中的表单域 
var txt=document.forms[0].elements["txt1"]; 
var btnStart=document.forms[0].elements["btnStart"]; 
var btnReset=document.forms[0].elements["btnReset"] 
//定义定时器的id 
var id; 
//每10毫秒该值增加1 
var seed=0; 
btnStart.onclick=function(){ 
  //根据按钮文本来判断当前操作 
  if(this.value=="开始"){ 
    //使按钮文本变为停止 
    this.value="停止"; 
    //使重置按钮不可用 
    btnReset.disabled=true; 
    //设置定时器,每0.01s跳一次 
    id=window.setInterval(tip,10); 
  }else{ 
    //使按钮文本变为开始 
    this.value="开始"; 
    //使重置按钮可用 
    btnReset.disabled=false; 
    //取消定时 
    window.clearInterval(id); 
  } 
} 
//重置按钮 
btnReset.onclick=function(){ 
  seed=0; 
} 
//让秒表跳一格 
function tip(){ 
  seed++; 
  txt.value=seed/100; 
} 
//--> 
</script>
Copy after login

I hope this article will be helpful to everyone learning JavaScript programming.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template