Implementation code of countdown in WeChat applet

不言
Release: 2018-09-06 10:26:27
Original
3726 people have browsed it

The content of this article is about the implementation code of countdown in WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In the product information of the mall, there will be a countdown function.

Implementation code of countdown in WeChat applet

The calculation time needs to be converted into a timestamp, but Android and iOS systems have different time formats for recognition, and Android has no requirements for recognition. IOS format requirements 2018/08/20 10:20:32, using Date.parse() to convert the timestamp will not appear on the IOS side and cannot count down.

timeFormat: function(param) { //小于10的格式化函数
    return param < 10 ? &#39;0&#39; + param : param;
  },
  countDown: function() { //倒计时函数
    // 获取当前时间,同时得到活动结束时间数组
    let newTime = Date.parse(new Date());
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];
    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      var strtime = o.replace(/-/g, &#39;/&#39;);
      strtime = strtime.substring(0, 19);
      let endTime = new Date(strtime).getTime();
      let obj = null;
      // 如果活动未结束,对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else { //活动已结束,全部设置为&#39;00&#39;
        obj = {
          day: &#39;00&#39;,
          hou: &#39;00&#39;,
          min: &#39;00&#39;,
          sec: &#39;00&#39;
        }
      }
      countDownArr.push(obj);
    })
    // 渲染,然后每隔一秒执行一次倒计时函数
    this.setData({
      countDownList: countDownArr
    })
    setTimeout(this.countDown, 1000);
  },
Copy after login

Related recommendations:

JS-based countdown program example_javascript skills

##jQuery group purchase countdown special effect implementation method _jquery

The above is the detailed content of Implementation code of countdown in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template