JS implements a stopwatch timer

JS implements a stopwatch timer

This article example shares the specific code of JS to implement the stopwatch timer for your reference. The specific content is as follows

Implementation of stopwatch timer:

The effect diagram is as follows:

Attached code, debugged and run

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    #div1 {
      width: 300px;
      height: 400px;
      background: skyblue;
      margin: 100px auto;
      text-align: center;
    }
    
    #count {
      width: 200px;
      height: 150px;
      line-height: 150px;
      margin: auto;
      font-size: 40px;
    }
    
    #div1 input {
      width: 150px;
      height: 40px;
      background: orange;
      font-size: 25px;
      margin-top: 20px
    }
  </style>
</head>

<body>
  <div id="div1">
    <div id="count">
      <span id="id_H">00</span>
      <span id="id_M">00</span>
      <span id="id_S">00</span>
    </div>
    <input id="start" type="button" value="Start">
    <input id="pause" type="button" value="Pause">
    <input id="stop" type="button" value="Stop">
  </div>
  <script>
    //You can simplify the operation of finding label nodes var btn = getElementById('btn')
    function $(id) {
      return document.getElementById(id)
    }
    window.onload = function() {
      //Click to start building and counting var count = 0
      var timer = null //The timer variable records the return value of the timer setInterval $("start").onclick = function() {
        timer = setInterval(function() {
          count++;
          console.log(count)
            // Need to change the value of hours, minutes and seconds on the page console.log($("id_S"))
          $("id_S").innerHTML = showNum(count % 60)
          $("id_M").innerHTML = showNum(parseInt(count / 60) % 60)
          $("id_H").innerHTML = showNum(parseInt(count / 60 / 60))
        }, 1000)
      }
      $("pause").onclick = function() {
          //Cancel the timer clearInterval(timer)
        }
        //Stop counting, clear data, and clear page display data$("stop").onclick = function() {
        //Cancel the timer $("pause").onclick()
          // clearInterval(timer)
          //Data clear total seconds clear count = 0
          // Page display data cleared $("id_S").innerHTML = "00"
        $("id_M").innerHTML = "00"
        $("id_H").innerHTML = "00"
      }

      //Encapsulate a function that processes single-digit numbers function showNum(num) {
        if (num < 10) {
          return '0' + num
        }
        return num
      }
    }
  </script>
</body>

</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue.js implements simple timer function
  • Implementing a simple timer in JavaScript
  • JS uses setInterval timer to implement the 10-second challenge
  • JavaScript implements a good-looking stopwatch timer
  • JavaScript setInterval() vs setTimeout() Timers
  • js implements built-in timer
  • Detailed explanation of JavaScript timer and button effect settings

<<:  mysql5.7.18.zip Installation-free version configuration tutorial (windows)

>>:  Detailed explanation of Linx awk introductory tutorial

Recommend

MySQL 8.0.12 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Design and implementation of Vue cascading drop-down box

Table of contents 1. Database design 2. Front-end...

Installation process of MySQL5.7.22 on Mac

1. Use the installation package to install MySQL ...

Docker private warehouse harbor construction process

1. Preparation 1.1 harbor download harbor downloa...

Detailed explanation of the use of find_in_set() function in MySQL

First, let’s take an example: There is a type fie...

In-depth analysis of the role of HTML <!--...--> comment tags

When we check the source code of many websites, w...

Summary of MySQL usage specifications

1. InnoDB storage engine must be used It has bett...

Can Docker become the next "Linux"?

The Linux operating system has revolutionized the...

Linux concurrent execution is simple, just do it this way

Concurrency Functions time for i in `grep server ...

How to hide and forge version number in Nginx

1. Use curl command to access by default: # curl ...

How to write memory-efficient applications with Node.js

Table of contents Preface Problem: Large file cop...

Detailed usage of js array forEach instance

1. forEach() is similar to map(). It also applies...

A very detailed summary of communication between Vue components

Table of contents Preface 1. Props, $emit one-way...

Vue implements the full selection function

This article example shares the specific code of ...