js实现倒计时

12 篇文章 0 订阅

 

<body contenteditable="true" spellcheck="false" style="height: 400px;">                 
    <div class="timespan" style="background:#F5EEF9;width: 100%;padding: 5px;height:30px;"></div>
    </body>

 

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
        //当前时间
            var timestamp = new Date().getTime()/1000;
            //秒杀开始时间
            var startTime=${seckillDate.result[0].start_time};
            //秒杀结束时间
            var endTime=${seckillDate.result[0].end_time};
            //秒杀当前数量
            var secnum=${seckillDate.result[0].seckill_number};
            var intDiff = parseInt(startTime-timestamp);//倒计时总秒数量
            var endDiff = parseInt(endTime-timestamp);//结束倒计时总秒数量
            function timer(intDiff){
                if(intDiff>=0&&intDiff<=60){
                window.setInterval(function(){
                var day=0,
                    hour=0,
                    minute=0,
                    second=0;//时间默认值        
                if(intDiff > 0){
                    day = Math.floor(intDiff / (60 * 60 * 24));
                    hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
                    minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
                    second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
                }
                    
                    
                if (minute <= 9) minute = '0' + minute;
                if (second <= 9) second = '0' + second;
                $('.timespan').html("倒计时:"+ second + "秒");
                intDiff--;
               if(intDiff<0){
                   location.reload();
               }
                }, 1000);
             
            }
                if(intDiff > 60){
                    var st=$('#startTime').val();
                    $('.timespan').html("开始时间:"+st);
                }
                if(intDiff<0&&endDiff>0){
                    if(secnum>0){
                        $('.timespan').hide();
                         
                    }else{
                        $('.timespan').html("商品已售完");
                    }
                    
            }
                
                if(endDiff<0){
                     $('.timespan').html("秒杀已结束");
            }
                }
            $(function(){
                timer(intDiff);
            }); 
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮生若梦l

你的鼓励实我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值