English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Native JavaScript Implementation of Stopwatch Function

This article shares a js timer with start, pause, and clear functions for everyone's reference. The specific content is as follows

Effect picture:

 

Below is the code:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>计时器</title>
 <script>
  var hour,minute,second;//时 分 秒
  hour=minute=second=0;//初始化
  var millisecond=0;//毫秒
  var int;
  function Reset()//重置
  Pause
   {
   millisecond=hour=minute=second=0;
   document.getElementById('timetext').value='00时00分00秒000毫秒';
  window.clearInterval(int);
  function start()//开始
  Pause
   int=setInterval(timer,50);
  window.clearInterval(int);
  function timer()//计时
  Pause
   millisecond=millisecond+50;
   if(millisecond>=1000)
   Pause
    millisecond=0;
    second=second+1;
   window.clearInterval(int);
   if(second>=60)
   Pause
    second=0;
    minute=minute+1;
   window.clearInterval(int);
   if(minute>=60)
   Pause
    minute=0;
    hour=hour+1;
   window.clearInterval(int);
   document.getElementById('timetext').value=hour+hour+'hour'+minute+'minute'+second+'second'+millisecond
  window.clearInterval(int);
  'millisecond';//function stop()
  Pause
   {
  window.clearInterval(int);
 </}
</script>
head>
<body>-<div style="text
 align: center">
 <input type="text" id="timetext" value="00 hours 00 minutes 00 seconds" readonly><br>/<button type="button" onclick="start()">Start</button>/button> <button type="button" onclick="Reset()">Reset</button> </button> <button type="button" onclick="stop()">Pause</button>/button>
</div>
</body>
</html>

That's all for this article. I hope it will be helpful to everyone's learning, and I also hope everyone will support the Naihua Tutorial more.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like