其他分享
首页 > 其他分享> > 前端页面展示时分秒倒计时

前端页面展示时分秒倒计时

作者:互联网

var openingCountDownTimer;//倒计时定时器
    var timerDifference;
    var serverTime;

$(function (){
    var targetTime = $("#targetTime").val();//目标时间
        var targetTimeNew = eval('new Date(' + quoteLimitTime.replace(/\d+(?=-[^-]+$)/,
            function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')').getTime();
        openingCountDownTimer = showOpeningTimeLimit(targetTimeNew);

}

function showOpeningTimeLimit(timer){
        var currentDate = new Date().getTime();//当前时间
        $.ajax({type:'options',url:'/',complete:function(x){
            serverTime = new Date(x.getResponseHeader('Date')).getTime();//服务器时间
            timerDifference = serverTime - currentDate;//计算时间差

            var nMS = timer - (currentDate + timerDifference);//计算倒计时毫秒数
            var nD = Math.floor(nMS/(1000 * 60 * 60 * 24));
            var nH = Math.floor(nMS/(1000*60*60)) % 24;
            var nM = Math.floor(nMS/(1000*60)) % 60;
            var nS = Math.floor(nMS/1000) % 60;

            if(nMS > 0){
                /**时间校正,防止本地时间被修改*/
                /*if(Math.abs(currentDate - timeTemp) > 2000){
                    loadTimeLimit();
                }*/

                var nT = '';
                if(nD > 0){
                    nT += nD + '天';
                }
                if(nH > 0){
                    nT += nH + '时';
                }
                if(nM > 0){
                    nT += nM + '分';
                }
                if(nS > 0){
                    nT += nS + '秒';
                }
                document.getElementById('targetTimeDiv').innerHTML=nT;
                openingCountDownTimer = setTimeout(function(){showOpeningTimeLimit(timer)},1000);//每秒刷新一次
            }else{
                document.getElementById('targetTimeDiv').innerHTML='倒计时已结束';
            }
        }});
    }

  

标签:60,nMS,倒计时,var,1000,nT,时分秒,Math,页面
来源: https://www.cnblogs.com/lovefaner/p/16136558.html