欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现倒计时(一个页面多个倒计时)

程序员文章站 2022-05-06 22:45:26
js实现倒计时(一个页面多个倒计时) $p.end_time:后台传来的时间戳 data-timestamp:自定义倒计时属性 --> $.each($(".da...

js实现倒计时(一个页面多个倒计时)

$p.end_time:后台传来的时间戳 data-timestamp:自定义倒计时属性 -->
        $.each($(".day-countdown"),function(){
            var timer = [];
            timer.push($(this).data("timestamp"));
            var pid = [];
            pid.push($(this).attr("id"));
            var date = new date(parseint(timer)*1000);
            if(timer!=0){
                setinterval(function(){
                    showcountdown(
                        date.getfullyear(),
                        date.getmonth()+1,
                        date.getdate(),date.gethours(),
                        date.getminutes(),
                        date.getseconds(),
                        pid);
                },1000);
            }
        });
        function showcountdown(year,month,day,h,m,s,pname)
        {
            var now = new date();
            var enddate = new date(year, month-1, day,h,m,s);
            var lefttime=enddate.gettime()-now.gettime();
            var leftsecond = parseint(lefttime/1000);
            var day1=checktime(math.floor(leftsecond/(60*60*24)));
            var hour=checktime(math.floor((leftsecond-day1*24*60*60)/3600));
            var minute=checktime(math.floor((leftsecond-day1*24*60*60-hour*3600)/60));
            var second=checktime(math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60));
            var cc = document.getelementbyid(pname);
            if(day1 <= 0 && hour <= 0 && minute <=0 && second <= 0){
                window.clearinterval();
            }else if(day1 === "0-1" || hour === "0nan" || minute === "0nan" || second === "0nan"){
                window.clearinterval();
            }else if(day1 == "00" && (hour != "0" || minute != "0" || second != "0")){
                cc.innerhtml = hour+":"+minute+":"+second; //有效期在24小时内开始时分秒倒计时
            }else{
                if(cc != null){
                    cc.innerhtml = parseint(day1)+"天"; //有效期在24小时外以天为单位进行倒计时
                }
            }
        }

        function checktime(i){
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }