javascript 实现时间倒计时效果

有时候在制作系统中,我们需要用到时间倒计时的使用。比如考试,如果到时自动提交

 var timeShow = '';
       /*设置2分倒计时*/
        var time1 =new Date().getTime() + (2 * 1000 * 60);;
        var timer=setInterval(() =>
        {
            timeShow = getTimeLeft(time1);
            console.log(timeShow);
            if (timeShow== "0分0秒") {
                clearInterval(timer);
            }
        }, 1000);
        //取倒计时(天时分秒)
        function getTimeLeft(time1) {
            // 计算目标与现在时间差(毫秒)
           // let time1 = new Date().getTime()+(45*1000*60);
            let time2 = new Date().getTime();
            let mss = time1 - time2;

            // 将时间差(毫秒)格式为:天时分秒
           // let days = parseInt(mss / (1000 * 60 * 60 * 24));
          //  let hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            let minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
            let seconds = parseInt((mss % (1000 * 60)) / 1000);

            return  minutes + "" + seconds + ""
        }

效果:

参考:https://www.cnblogs.com/baqiphp/p/6145450.html

原文地址:https://www.cnblogs.com/fogwang/p/12800081.html