js(借助jquery库)实现倒计时

不考虑样式,在此上一段 js倒计时的代码

function countDown(){
    var time_now = new Date();
    var time_end = new Date(2017, 0, 6,21,29,59);//月份记得减1
    time_end = time_end.valueOf();
    time_now = time_now.valueOf();
    var time_diff = time_end - time_now;
    if((time_diff - 1000) < 0){//倒计时已结束,根据需要自行调整
        clearInterval(count_down_timer);
        $('.count-day').html('0');
        $('.count-hour').html('0');
        $('.count-min').html('0');
        return;
    }
    time_diff = new Date(time_diff);

    var diff_day = (time_diff.getDate()-1).toString();//天
    var diff_hour = (time_diff.getHours()-8).toString();//时
    var diff_minute = (time_diff.getMinutes()).toString();//分 
    var diff_second = (time_diff.getSeconds()).toString();//秒
}
$(function(){
    countDown();
    count_down_timer = setInterval('countDown()',1000);//每秒轮询一次
});

这是用在距离倒计时的日子还有多少天、时、分、秒。


<html>
<head>
    <title>倒计时title>
    <meta charset="utf-8">
head>
<body>
    <script type="text/javascript">
        var SECOND_BASE = 1000;
        var MINUTE_BASE = 60 * SECOND_BASE;
        var HOUR_BASE = 60 * MINUTE_BASE;
        var DAY_BASE = 24 * HOUR_BASE;
        function getCountDown(time_end){
            var time_now = new Date();
            time_end = time_end.valueOf();
            time_now = time_now.valueOf();
            diff = time_end - time_now;
            var result = [0,0,0,0];
            if(diff < 1000){
                result[0] = -1;
            }else{
                result[0] = parseInt(diff / DAY_BASE);
                diff %= DAY_BASE;
                result[1] = parseInt(diff / HOUR_BASE);
                diff %= HOUR_BASE;
                result[2] = parseInt(diff / MINUTE_BASE);
                diff %= MINUTE_BASE;
                result[3] = parseInt(diff / SECOND_BASE);
            }
            return result;
        }

        window.onload = function() {
            var res = getCountDown(new Date(2017, 0, 6,21,29,59));
            console.log(res);
        }
    script>
body>
html>

你可能感兴趣的:(前端,javascript,倒计时)