小程序学习:倒计时案例

小程序自学网站推荐:http://www.51zxw.net/study.asp?vip=4857021

wxml:

<button bindtap='begin'  type='warn'>开始</button>

<view>{{time}}</view>

wxss:

button{
    font-size:30px;
}

view{
    text-align: center;
    margin-top: 100rpx;
    font-size:30px;
    color: #ff6000;
}

js:

var allMil = 48;   //48小时倒计时
allMil = allMil*3600*1000;


function toTow(num){
    if (num<10){
        return "0"+num;
    }else{
        return num;
    }
};

function format(mil){
    var allSecond = Math.floor(mil/1000);     //    总的秒数
    var h = Math.floor(allSecond / 3600);   //    时
    var m = Math.floor((allSecond - h * 3600) / 60);    //    分
    var s = Math.floor(allSecond - h * 3600 - m * 60);     //    秒

    h = toTow(h);   //    时
    m = toTow(m);      //    分
    s = toTow(s);      //    秒

    return h + ":" + m + ":"+s ;
};



Page({


  data: {
      time:"本次活动已结束!",
  },

  begin:function(){

      var temp = format(allMil);

      this.setData({
          time: temp,
      });

      if (allMil<0){

          this.setData({
              time: "本次活动已结束!",
          });
            return;
        };

       setTimeout(
           ()=>{
               allMil-=1000;
               this.begin();
           },1000
       );



  },

 
})

效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值