小程序倒计时秒杀

1:小程序实现电商秒杀倒计时效果+样式

wxml:

<view class="container">
     <text>淘抢购倒计时: {{second}} </text>
</view>

wxss:

.container{
  background: #fe6906;
  color: #ffffff;
}

js

// 从从60到到0倒计时
function countdown(that) {
  var second = that.data.second
  if (second == 0) {
    that.setData({
      second: "秒杀结束"
    });
    return;
  }
  var time = setTimeout(function () {
    that.setData({
      second: second - 1
    });
    countdown(that);
  }
    , 1000)
}

Page({
  data: {
    second: 60
  },
  onLoad: function () {
    countdown(this);
  }
});

效果如下

rnh03tG7ni.gif

2:时分秒倒计时+样式
拼团秒杀功能
https://blog.csdn.net/qq_41473887/article/details/81287786

wxml:

<view class='container'>
    <text>剩余时间:{{countdown}}</text>
</view>

wxss:

.container{
  background: #fe6906;
  color: #ffffff;
}

js:

扫描二维码关注公众号,回复: 6125926 查看本文章
Page({

  /*页面的初始数据*/
  data: {
    countdown: ''
    , endDate2: '2018-11-11 11:41:00'
  },
  /* 生命周期函数--监听页面加*/
  onLoad: function (options) {
    var that = this;
    that.countTime()
  },
  countTime() {
    var that = this;
    var date = new Date();
    var now = date.getTime();
    var endDate = new Date(that.data.endDate2);//设置截止时间
    var end = endDate.getTime();
    var leftTime = end - now; //时间差                              
    var d, h, m, s, ms;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
      ms = Math.floor(leftTime % 1000);
      ms = ms < 100 ? "0" + ms : ms
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      h = h < 10 ? "0" + h : h
      that.setData({
        countdown: d + ":" + h + ":" + m + ":" + s + ":" + ms,
      })
      //递归每秒调用countTime方法,显示动态时间效果
      setTimeout(that.countTime, 100);
    } else {
      console.log('已截止')
      that.setData({
        countdown: '00:00:00'
      })
    }

  },
})

效果如下:

原文作者:祈澈姑娘

猜你喜欢

转载自blog.csdn.net/weixin_44763569/article/details/89679935