小程序实现简单的倒计时秒杀效果

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:

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'
      })
    }

  },
})

效果如下:

原文作者:祈澈姑娘 技术博客:https://www.jianshu.com/u/05f416aefbe1
90后前端妹子,爱编程,爱运营,爱折腾。坚持总结工作中遇到的技术问题,坚持记录工作中所所思所见,欢迎大家加入群聊,一起探讨交流。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 每天的学习记录,可能有的地方写的不对,因为刚学,以后发现错的话会回来改掉整体流程 https://develope...
    有点健忘阅读 10,352评论 0 7
  • create by jsliang on 2018-9-17 17:58:56 Recently revised...
    梁_飘阅读 5,532评论 0 6
  • 微信小程序的特点 小程序是一种不需要下载、安装即可使用的应用,它出现了触手可及的梦想,用户扫一扫或者搜一下即开打开...
    Simple_3f19阅读 4,417评论 0 0
  • 一、页面的实现 将页面分成三个页面,分别是index、change、history 将三个页面所需要的共同样式都写...
    sweetBoy_9126阅读 7,438评论 1 1
  • 在湘潭大山湯氏八修族譜即將修編之時,我們研究了“大山湯氏七修族谱”編寫的傳贊,發現祖先盛讚各派所出現之優秀人物,其...
    转发件阅读 3,890评论 0 0

友情链接更多精彩内容