美文网首页
微信小程序:倒计时实例

微信小程序:倒计时实例

作者: 空箜崆 | 来源:发表于2020-11-11 09:31 被阅读0次

话不多说,直接上代码

js文件:
//timer.js
class Timecut {
  constructor(concact) {
    this.lock = false;
    //是否展示天数
    this.showDay = concact.showDay || false

    this.argument = concact.argument
    //获取传入的截止日期
    this.send = concact.send //时间字符串
    this.milliEndTime = concact.milliEndTime //时间戳
    this.endCb = concact.endCb
    if (!this.send && !this.milliEndTime) throw new Error("空的结束日期");
    if (this.send && isNaN(this.send)) {
      this.send = this.send.replace(/-/g, '/')
    } else {
      this.send = this.milliEndTime
    }
    if (concact.start && isNaN(concact.start)) {
      this.now = new Date(concact.start.replace(/-/g, '/')).getTime()
    } else if (concact.start) {
      this.now = concact.start
    } else {
      this.now = new Date().getTime()
    }
    //获取函数外部的this
    var that = this

    //设置截止日期
    this.endDate = new Date(this.send)
    this.end = this.endDate.getTime();

    //时间差
    this.sendTime = (this.end - this.now) / 1000;

    //执行倒计时方法
    this.cut = setInterval(() => {
      that.setTime();
      this.sendTime--
    }, 1000)
  }

  setTime() {
    if (this.lock) { return false };
    if (this.sendTime >= 0) {
      this.d = this.formatTime(Math.floor(this.sendTime / 86400))
      this.h = this.showDay ? this.formatTime(Math.floor((this.sendTime % 86400) / 3600)) : this.formatTime(Math.floor(this.sendTime / 86400) * 24 + Math.floor((this.sendTime % 86400) / 3600))
      this.m = this.formatTime(Math.floor(((this.sendTime % 86400) % 3600) / 60))
      this.s = this.formatTime(Math.floor(((this.sendTime % 86400) % 3600) % 60))
    } else {
      this.clearTimer();
      this.endCb && this.endCb()
    }
    this.display([this.d||'00', this.h||'00', this.m||'00', this.s||'00'])
  }

  formatTime(val) {
    return val < 10 ? `0${val}` : '' + val
  }

  clearTimer() {
    clearInterval(this.cut)
    this.cut = null;
    this.lock = true;
  }

  display(time) {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 1];
    prevPage.setData({
      [this.argument]: time
    })
  }
}
export default Timecut;

使用传入的参数argument,通过获取到当前页面栈的实例 getCurrentPages(),回传倒计时;

再来看wxml的创建单个倒计时实例

import countDownTimer from 'timer.js'
data: {
  timeArr: null,
},
...
let set = {
  milliEndTime: new Date().getTime() + time * 1000,
  argument: 'timeArr',
  endCb: this.endCb,
}
...
endCb(){}
...
 this.countDownTimer = new countDownTimer(set);
...
//离开页面销毁倒计时
clearCountTimer() {
    if (this.countDownTimer) {
      this.countDownTimer.clearTimer();
      this.countDownTimer = null;
    }
 }

创建倒计时数列

import Timecut from "timer.js";
...
data: {
  cutdowns: [],
  countDownTimer: [],
},
...
setCutDown(list) {
  let cutdowns = this.data.countDownTimer
  let initIndex = cutdowns.length
  list.forEach((el, index) => {
      let cuttime = `cutdowns[${initIndex + index}]`
      let timer = new Timecut({
        argument: cuttime,
        start: el.systemTime,
        send: el.expireDate,
        endCb: (val) => this.endFunc(initIndex + index)
      })
      cutdowns.push({
        countDownTimer: timer
      })
  })
  this.setData({ countDownTimer: cutdowns })
},
...
  // 清除倒计时
  clearCountDownTimer() {
    let countDownTimer = this.data.countDownTimer
    countDownTimer.forEach(el => {
      if (el.countDownTimer) {
        el.countDownTimer.clearTimer()
        el.countDownTimer = null
      }
    })
    countDownTimer = []
    this.setData({ countDownTimer })
  },

相关文章

  • 微信小程序:倒计时实例

    话不多说,直接上代码 js文件: 使用传入的参数argument,通过获取到当前页面栈的实例 getCurrent...

  • 微信小程序实例

    UI组件 weui-wxss ★852- 同微信原生视觉体验一致的基础样式库 Wa-UI ★122- 针对微信小程...

  • 微信小程序实例

    写一些在编辑过程中的问题1.上传图片直接放在项目里是无效的 打开项目所在文件放进去才可以 2.toggle效果 w...

  • 微信小程序实例

    自己瞎捣鼓个微信小程序,源码 主要功能就是个新闻列表,美女图片列表。新闻详情是需要在webview中加载,这个暂时...

  • 微信小程序源码大全

    微信小程序实例源码大全下载 微信小应用示例代码(phodal/weapp-quick) 源码链接:https://...

  • 记工作学习中的坑 -微信小程序 全局变量的使用。

    微信小程序中 app.js 中添加全局变量; 在需要设置的页面中: getApp()可以获取这个微信小程序的实例化...

  • 微信小程序实现课程表实例

    前言 感谢! 承蒙关照~ 微信小程序实现课程表实例 效果图: 小程序代码如下

  • 微信小程序基础

    微信小程序介绍微信小程序开发工具的使用微信小程序框架文件微信小程序逻辑层微信小程序视图层微信小程序组件介绍微信小程...

  • 微信小程序源码实例

    微信小应用示例代码(phodal/weapp-quick)源码链接:https://github.com/phod...

  • 微信小程序项目实例

    https://blog.csdn.net/qq_27642629/article/details/81102299

网友评论

      本文标题:微信小程序:倒计时实例

      本文链接:https://www.haomeiwen.com/subject/hpxebktx.html