×

微信小程序:实现多个倒计时功能

作者:andy0012019.04.10来源:Web前端之家浏览:11864评论:0

这是微信小程序的一个倒计时代码片段,但在苹果手机里,这个倒计时并不会显示!!

wechatide://minicode/QUV0k2mG7R2Q

原因是在苹果手机里时间戳的转换问题!!

可兼容苹果安卓手机的代码,重点:

  1. let arr = that.data.gmtDate.split(/[- :]/);// that.data.gmtDate时间格式为'2018-08-07 10:23:00'
  2. let nndate = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
  3. nndate=Date.parse(nndate)
  4. let timeLeft = nndate- new Date();

以下 是兼容苹果安卓的js倒计时的微信小程序代码片段!!!

wechatide://minicode/E7jtE8mk7M2Z

代码详解:

JS代码:

  1. let goodsList = [{
  2.     actEndTime: '2019-05-01 10:00:43'
  3.   },
  4.   {
  5.     actEndTime: '2018-09-01 11:00:00'
  6.   },
  7.   {
  8.     actEndTime: '2018-06-01 12:45:56'
  9.   },
  10.   {
  11.     actEndTime: '2018-07-01 15:00:23'
  12.   },
  13.   {
  14.     actEndTime: '2018-11-23 17:00:22'
  15.   },
  16.   {
  17.     actEndTime: '2018-09-14 19:00:44'
  18.   },
  19.   {
  20.     actEndTime: '2018-10-21 21:00:34'
  21.   },
  22.   {
  23.     actEndTime: '2018-11-17 09:00:37'
  24.   },
  25.   {
  26.     actEndTime: '2018-12-21 05:00:59'
  27.   },
  28.   {
  29.     actEndTime: '2018-9-19 07:00:48'
  30.   },
  31.   {
  32.     actEndTime: '2018-10-28 03:00:11'
  33.   }
  34. ]
  35. Page({
  36.   data: {
  37.     countDownList: [],
  38.     actEndTimeList: []
  39.   },
  40.   onLoad() {
  41.     let endTimeList = [];
  42.     // 将活动的结束时间参数提成一个单独的数组,方便操作
  43.     goodsList.forEach(=> {
  44.       endTimeList.push(o.actEndTime)
  45.     })
  46.     this.setData({
  47.       actEndTimeList: endTimeList
  48.     });
  49.     // 执行倒计时函数
  50.     this.countDown();
  51.   },
  52.   timeFormat(param) { //小于10的格式化函数
  53.     return param < 10 ? '0' + param : param;
  54.   },
  55.   countDown() { //倒计时函数
  56.     // 获取当前时间,同时得到活动结束时间数组
  57.     let newTime = new Date().getTime();
  58.     let endTimeList = this.data.actEndTimeList;
  59.     let countDownArr = [];
  60.  
  61.     // 对结束时间进行处理渲染到页面
  62.     endTimeList.forEach(=> {
  63.       ///-----------------------------------------------------------------------------------------////
  64.  
  65.       //计算时间戳方式一,(ios有bug)
  66.       // let endTime = new Date(o).getTime();
  67.  
  68.  
  69.  
  70.       //计算时间戳方式二,(兼容ios和android)
  71.       let arr = o.split(/[- :]/);
  72.       let nndate = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
  73.       nndate = Date.parse(nndate)
  74.       let endTime = nndate;
  75.  
  76.  
  77.  
  78.  
  79.       ///----------------------------------------------------------------------------------------------------------///
  80.  
  81.  
  82.       let obj = null;
  83.       // 如果活动未结束,对时间进行处理
  84.       if (endTime - newTime > 0) {
  85.         let time = (endTime - newTime) / 1000;
  86.         // 获取天、时、分、秒
  87.         let day = parseInt(time / (60 * 60 * 24));
  88.         let hou = parseInt(time % (60 * 60 * 24) / 3600);
  89.         let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
  90.         let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
  91.         obj = {
  92.           day: this.timeFormat(day),
  93.           hou: this.timeFormat(hou),
  94.           min: this.timeFormat(min),
  95.           sec: this.timeFormat(sec)
  96.         }
  97.       } else { //活动已结束,全部设置为'00'
  98.         obj = {
  99.           day: '00',
  100.           hou: '00',
  101.           min: '00',
  102.           sec: '00'
  103.         }
  104.       }
  105.       countDownArr.push(obj);
  106.     })
  107.     // 渲染,然后每隔一秒执行一次倒计时函数
  108.     this.setData({
  109.       countDownList: countDownArr
  110.     })
  111.     setTimeout(this.countDown, 1000);
  112.   }
  113. })

WXML:

  1. <view class='tui-countdown-content' wx:for="{{countDownList}}" wx:key="countDownList">
  2.   剩余
  3.   <text class='tui-conutdown-box'>{{item.day}}</text>
  4.   <text class='tui-conutdown-box'>{{item.hou}}</text>
  5.   <text class='tui-conutdown-box'>{{item.min}}</text>
  6.   <text class='tui-conutdown-box tui-countdown-bg'>{{item.sec}}</text>
  7. </view>

CSS

  1. page{background-color: #eee;}
  2. .tui-countdown-content{
  3.   height: 50px;
  4.   line-height: 50px;
  5.   text-align: center;
  6.   background-color: #fff;
  7.   margin-top: 15px;
  8.   padding: 0 15px;
  9.   font-size: 18px;
  10. }
  11. .tui-conutdown-box{
  12.   display: inline-block;
  13.   height: 26px;
  14.   width: 26px;
  15.   line-height: 26px;
  16.   text-align: center;
  17.   background-color: #000;
  18.   color: #fff;
  19.   margin: 0 5px;
  20. }
  21. .tui-countdown-bg{
  22.   background-color: #DF0101;
  23. }

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/XIAOCH20-394-2934-923-49.html

网友评论文明上网理性发言 已有0人参与

发表评论: