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

Create a simple countdown function with JS

巴扎黑
Release: 2017-07-21 17:23:03
Original
1547 people have browsed it
<script>
  (function () {

    var tian = document.getElementsByClassName('JS-tian')[0];
    var shi = document.getElementsByClassName('JS-shi')[0];
    var fen = document.getElementsByClassName('JS-fen')[0];
    var miao = document.getElementsByClassName('JS-miao')[0];

    var endTime = new Date('2117/07/12 23:59:59').getTime() + 1000;

    var interval = null;
    interval = setInterval(function () {
      var syhm = endTime - Date.now(); // 剩余毫秒
      if (syhm >= 0) {
        tian.innerText = Math.floor(syhm / 1000 / 60 / 60 / 24);
        shi.innerText = Math.floor(syhm / 1000 / 60 / 60 % 24);
        fen.innerText = Math.floor(syhm / 1000 / 60 % 60);
        miao.innerText = Math.floor(syhm / 1000 % 60);
      } else {
        clearInterval(interval);
      }
    }, 0);

  })();
</script>
Copy after login
 
 
htmlDemo:
 
<div>
  距结束还剩:<span class="JS-tian"></span>天<span class="JS-shi"></span>时<span class="JS-fen"></span>分<span class="JS-miao"></span>秒
</div>
Copy after login

The above is the detailed content of Create a simple countdown function with JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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