Home  >  Article  >  Web Front-end  >  Create a simple countdown function with JS

Create a simple countdown function with JS

巴扎黑
巴扎黑Original
2017-07-21 17:23:031488browse
<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>
 
 
htmlDemo:
 
<div>
  距结束还剩:<span class="JS-tian"></span>天<span class="JS-shi"></span>时<span class="JS-fen"></span>分<span class="JS-miao"></span>秒
</div>

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!

Statement:
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