search
HomeWeChat AppletMini Program DevelopmentWeChat Mini Program Example: How to implement batch countdown (with code)

This article brings you an example of WeChat applet: how to implement batch countdown (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

//Applicable to product list countdown/*** end_time int end time * param int array key*/

1. The display effect is as follows:

# #2.wxml code:

<p class="promotion-label-tits">仅{{item.endtime}}</p>

3.js code:

//封装的倒计时方法
//批量倒计时
function grouponcountdown(that, end_time, param) {
  var EndTime = new Date(end_time).getTime();
  // console.log(EndTime);
  var NowTime = new Date().getTime();

  var total_micro_second = EndTime - NowTime;

  var groupons = that.data.groupon;
  // console.log(groupons);
  groupons[param].endtime = dateformats(total_micro_second);
  if (total_micro_second <= 0) {
    groupons[param].endtime = "已结束"
  }
  that.setData({
    groupon: groupons
  })
  setTimeout(function () {
    grouponcountdown(that, end_time, param);
  }, 1000)
}

// 时间格式化输出,每1s都会调用一次
function dateformats(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  var hrStr = hr.toString();
  if (hrStr.length == 1) hrStr = &#39;0&#39; + hrStr;

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

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

  if (day <= 1) {
    return "剩 " + hrStr + ":" + minStr + ":" + secStr;
  } else {
    return "剩 " + day + " 天 " + hrStr + ":" + minStr + ":" + secStr;
  }
}
//end

var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      collageTeamlist : {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      app.showLoading();
      var that = this
     
      wx.request({
          success:function(res){
              var grouponList = request.data.collageTeamlist
              // console.log(grouponList);
              for (var i = 0; i < grouponList.length; i++) {
                 var lack_num = grouponList[i].create_num - grouponList[i].current_num
                 grouponList[i].lack_num = lack_num
              }

              that.setData({
                  groupon: grouponList
              })
              var data = that.data.groupon
              //列表获取到数据进行遍历
              for (var i = 0; i < data.length; i++) {
                  var end_time = data[i].end_time.replace(/-/g, &#39;/&#39;)
                  grouponcountdown(that,end_time, i)
              }
          },
      })
  },

Related recommendations:

Example of WeChat applet: code to implement pull-down refresh data

Mini Program: Code to implement click countdown

Mini Program Component: Introduction to Chat Session Component (with code)

The above is the detailed content of WeChat Mini Program Example: How to implement batch countdown (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use