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 Packet for query is too large problem and solution

Problem description: Error message: Caused by: co...

Tutorial on using prepare, execute and deallocate statements in MySQL

Preface MySQL officially refers to prepare, execu...

Three examples of blur background effects using CSS3

Let’s not start with the introduction and get str...

Let you understand the working principle of JavaScript

Table of contents Browser kernel JavaScript Engin...

The most comprehensive collection of front-end interview questions

HTML+CSS 1. Understanding and knowledge of WEB st...

Delegating Privileges in Linux Using Sudo

Introduction to sudo authority delegation su swit...

Front-end JavaScript operation principle

Table of contents 1. What is a JavaScript engine?...

jQuery realizes the picture following effect

This article shares the specific code of jQuery t...

Bootstrap FileInput implements image upload function

This article example shares the specific code of ...

Some conclusions on the design of portal website focus pictures

Focus images are a way of presenting content that ...

Solution to MySQL error code 1862 your password has expired

The blogger hasn't used MySQL for a month or ...

How to get/calculate the offset of a page element using JavaScript

question By clicking a control, a floating layer ...

Tutorial on using hyperlink tags in XHTML

Hyperlink, also called "link". Hyperlin...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...