JSはストップウォッチタイマーを実現します。

3412 ワード

本論文の実例はJSが秒計タイマーを実現する具体的なコードを共有しています。
ストップウォッチタイマの実現:
効果図は以下の通りです

コードを添付し、デバッグ実行しました。

<!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="  ">
    <input id="pause" type="button" value="  ">
    <input id="stop" type="button" value="  ">
  </div>
  <script>
    //                 var btn = getElementById('btn')
    function $(id) {
      return document.getElementById(id)
    }
    window.onload = function() {
      //          
      var count = 0
      var timer = null //timer       setInterval    
      $("start").onclick = function() {
        timer = setInterval(function() {
          count++;
          console.log(count)
            //             
          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() {
          //     
          clearInterval(timer)
        }
        //                  
      $("stop").onclick = function() {
        //     
        $("pause").onclick()
          // clearInterval(timer)
          //          
        count = 0
          //        
        $("id_S").innerHTML = "00"
        $("id_M").innerHTML = "00"
        $("id_H").innerHTML = "00"
      }

      //             
      function showNum(num) {
        if (num < 10) {
          return '0' + num
        }
        return num
      }
    }
  </script>
</body>

</html>
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。