Home > Web Front-end > JS Tutorial > JS implements countdown function based on user input minutes

JS implements countdown function based on user input minutes

高洛峰
Release: 2016-12-07 09:23:39
Original
1663 people have browsed it

No more nonsense, let me just post the code for you. The specific code is as follows:

In fact, I have come across a lot of this countdown before, but I just used other people’s source code.
In response to the needs of the project, I finally took it seriously and took a week to solve a seemingly simple problem.
The source code can be copied and pasted directly.

Redundant version + simplified version.

<!DOCTYPE html> 
<html> 
  <head> 
    <meta charset="utf-8"> 
  <title></title> 
 </head> <body> 
  <script type="text/javascript"> 
    var createTime = &#39;2016-11-14 10:20:00&#39;;//开始时间 
    var limitTimes = 10;//时间长度 
    // 倒计时 入口 
    countdowns = window.setInterval(function(){ 
      var arr = cutDoowns(limitTimes,createTime); 
      document.write(formatDate(arr[0])+&#39;:&#39;+formatDate(arr[1])+&#39;:&#39;+formatDate(arr[2])+&#39;</br>&#39;); 
      if(arr[2]){ 
        document.write(&#39;时间到!&#39;); 
      } 
    },1000); 
  
    /* 
      s,10分钟后的具体日期: 
      date,开始时间 
      然后转化成毫秒比较,所得的差值化成分秒,就是倒计时的分秒。 
     */
    function cutDoowns(s,date){ 
      console.log(&#39;&#39;); 
      var flag = false; 
      var arr = [];//arr[0]:分,arr[1]:秒,arr[2]:返回boolean 
      var now = new Date();//当前时间 
      var now1 = new Date(date);//开始时间 
      console.log(&#39;开始时间 now1: &#39;+now1); 
      now1.setMinutes(now1.getMinutes()+s);//10分钟后的时间 
      console.log(&#39;当前时间 now :&#39;+now); 
      console.log(&#39;10分钟时 now1:&#39;+now1); 
  
      // 转化成年月日 时分秒 格式 
      var n = now.getFullYear()+&#39;/&#39;+(now.getMonth()+1)+&#39;/&#39;+now.getDay()+&#39; &#39;+now.getHours()+&#39;:&#39;+now.getMinutes()+&#39;:&#39;+now.getSeconds(); 
      var n1 = now1.getFullYear()+&#39;/&#39;+(now1.getMonth()+1)+&#39;/&#39;+now1.getDay()+&#39; &#39;+now1.getHours()+&#39;:&#39;+now1.getMinutes()+&#39;:&#39;+now1.getSeconds(); 
      // 日期转化成毫秒 
      var time1 = (new Date(n)).getTime(); 
      var time2 = (new Date(n1)).getTime(); 
      // 毫秒转日期格式 
      var time11 = new Date(time1); 
      var time21 = new Date(time2); 
      console.log(&#39;当前时间:&#39;+n+&#39;,转化成毫秒:&#39;+time1+&#39;,time11:&#39;+time11); 
      console.log(&#39;10分钟时:&#39;+n1+&#39;,转化成毫秒:&#39;+time2+&#39;,time21:&#39;+time21); 
  
      var surplusSec = time2-time1;//距离解锁剩余毫秒 
  
      if(surplusSec<=0){ 
        clearInterval(countdowns); 
        flag = true; 
        return arr = [00,00,flag]; 
      } 
  
      var minute = Math.floor(surplusSec/1000/60);//分钟 
      var second = Math.floor((surplusSec-minute*60*1000)/1000);//剩余秒数 
      console.log(&#39;剩余时间,minute: &#39;+minute+&#39;,second: &#39;+second+&#39;,surplusSec:&#39;+surplusSec); 
      // var second = Math.round(surplusTimes);//秒数 
      console.log(&#39;剩余时间,minute: &#39;+minute+&#39;,second: &#39;+second+&#39;,surplusSec:&#39;+surplusSec); 
  
      arr = [minute,second,flag]; 
      return arr; 
    } 
  
    //格式化日期:把单字符转成双字符 
    function formatDate(n){ 
      n = n.toString(); 
      // console.log(n); 
      if(n.length<=1){ 
        n = &#39;0&#39;+n; 
      } 
      // console.log(n); 
      return n; 
    } 
  </script> 
  </body> 
</html>
Copy after login

Simplified version:

<!DOCTYPE html> 
<html> 
  <head> 
    <meta charset="utf-8"> 
    <title>打开调试工具,看效果!</title> 
  </head> 
  <body> 
  
  <script type="text/javascript"> 
    /* 
      打开调试工具,看效果! 
      思路: 
      1.设置一个倒计时的时间长度; 
      2.设置开始时间和当前时间; 
      3.结束时间是 开始时间+倒计时间; 
      4.结束毫秒-开始毫秒=剩余倒计时间。 
     */
      
    // 准备 
    var countdownMinute = 10;//10分钟倒计时 
    var startTimes = new Date(&#39;2016-11-16 15:55&#39;);//开始时间 new Date(&#39;2016-11-16 15:21&#39;); 
    var endTimes = new Date(startTimes.setMinutes(startTimes.getMinutes()+countdownMinute));//结束时间 
    var curTimes = new Date();//当前时间 
    var surplusTimes = endTimes.getTime()/1000 - curTimes.getTime()/1000;//结束毫秒-开始毫秒=剩余倒计时间 
      
    // 进入倒计时 
    countdowns = window.setInterval(function(){ 
      surplusTimes--; 
      var minu = Math.floor(surplusTimes/60); 
      var secd = Math.round(surplusTimes%60); 
      console.log(minu+&#39;:&#39;+secd); 
      if(surplusTimes<=0){ 
        console.log(&#39;时间到!&#39;); 
        clearInterval(countdowns); 
      } 
    },1000); 
  
  
  </script> 
  </body> 
</html>
Copy after login


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