倒计时

function countdown(){
    var countdown = $(".header .time"),
        reduceDay = countdown.children(".countdown"),
        seed = 0,
        startTime = (+new Date(2015,1,13,18,0,0,0)) / 1000;
    function numFormat(num){
        if(num < 10){
            return "0" + num;
        }else{
            return num;
        }
    }
    function timeEnd(){
        reduceDay.text("00");
    }
    function setRemainTime(time){
        time = time - seed;
        if (time > 0) {
            var day = Math.floor((time / 3600) / 24);
            reduceDay.text(numFormat(day));
        } else {
            timeEnd();
        }
        seed++;
    }
    function ajaxHandle(xhr){
        var headers = xhr.getResponseHeader('Date'),
            tmp = Date.parse(headers) / 1000,
            reduce = startTime - tmp;
        if(reduce > 0) {
            countdown.attr('diff','');
            countdown.attr('diff', reduce);
            var time = parseInt(countdown.attr('diff'));
            setRemainTime(time);
            var InterValObj = window.setInterval(function(){setRemainTime(time)},1000);
        }
        else{
            timeEnd();
        }
    }
    $.ajax({
        type : "get",
        url : "http://ltt.1905.com/time.html",
        cache : false,
        success : function(data,status,xhr){
            ajaxHandle(xhr);
        },
        error : function(xhr){
            ajaxHandle(xhr);
        }
    });
}
countdown();
原文地址:https://www.cnblogs.com/jiangtuzi/p/4301699.html