小程序,商品秒杀倒计时效果

html部分:
<text>{{daojishi_show}}</text>

js部分:
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_time: "1560258000",
    end_time: "1560580000",
    daojishi_show:' ',
  },

  daojishi: function () {
    var timestamp = Date.parse(new Date());
    const that = this;
    timestamp = timestamp / 1000;

    var daojishi_show = this.data.daojishi_show;
    if (timestamp < that.data.start_time) {
      var daojishi = that.data.start_time - timestamp
      daojishi = daojishi
      that.data.daojishi_show = this.dateformats(daojishi, '')
      
    } else {
      var daojishi = that.data.end_time - timestamp;
      if (daojishi > 0) {
        daojishi = daojishi
        that.data.daojishi_show = this.dateformats(daojishi,'')
      } else {
        daojishi = daojishi
        that.data.daojishi_show = '已结束'
      }
    };
    this.setData({
      daojishi_show: that.data.daojishi_show
    })
  }, 
  dateformats(micro_second, title) {
    // 总秒数
    var second = Math.floor(micro_second);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    var hrStr = hr.toString();
    if (hrStr.length == 1) hrStr = '0' + hrStr;

    // 分钟
    var min = Math.floor(second / 60 % 60);
    var minStr = min.toString();
    if (minStr.length == 1) minStr = '0' + minStr;

    // 秒
    var sec = Math.floor(second % 60);
    var secStr = sec.toString();
    if (secStr.length == 1) secStr = '0' + secStr;

    if (day <= 1) {
      return title + " " + hrStr + ":" + minStr + ":" + secStr;
    } else {
      return title + " " + day + " 天 " + hrStr + ":" + minStr + ":" + secStr;
    }
  },  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    setInterval(this.daojishi, 1000);
    var that = this;
    var temp = WxParse.wxParse('content', 'html', replyHtml0, that, 10);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。