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

js millisecond countdown

巴扎黑
Release: 2016-11-25 14:57:44
Original
1292 people have browsed it

The refresh time is not necessarily 50 milliseconds, but considering the recognition ability of the human eye, it is meaningless to refresh the frequency too fast. In addition, we should consider two demand situations: 1. It is still far from a certain time in the future. How long; 2. How long has it been since a certain time in the past to now.

<div id="timeB"></div>
<script type="text/javascript">
    function countDown(endTime, startTime = new Date()) {//为了满足一些特殊情况这里给一个开始时间的参数并附上默认值,一般情况只需要传入结束时间即可
        let date = endTime - startTime; //时间差  
        let mmsec = date % 1000 //所余毫秒数  
        let seconds = Math.floor(date / 1000 % 60); //所余秒数  
        let minutes = Math.floor(date / 1000 / 60 % 60); //所余分钟数  
        let hour = Math.floor(date / 1000 / 60 / 60 % 24); //所余时钟数  
        let day = Math.floor(date / 1000 / 60 / 60 / 24); //天数  
        return {
            day: day
            , hour: hour
            , minutes: minutes
            , seconds: seconds
            , mmsec: mmsec
        }
    }
    const timeB = document.querySelector(&#39;#timeB&#39;);
    //开启定时器
    setInterval(() => {
        let time = countDown(new Date(2016, 8, 1));//只需要传入结束时间
        timeB.innerHTML = "距离2016年9月1号00点还有" + time.day + "天" + time.hour + "小时" + time.minutes + &#39;分钟&#39; + time.seconds + &#39;秒&#39; + time.mmsec + "毫秒";
    }, 50);
</script>
Copy after login


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