在使用之前需要先在page页引入wxTimer.js文件(这里我将文件放在/utils)

let timer = require('../../utils/wxTimer.js');

然后就可以使用啦

调用如下:

let wxTimer = new timer({
expired_at: "2018-9-27 23:28:00.14756",
complete: function () {
console.log("完成了")
},
expired: function () {
console.log("过期了(结束时间小于当前时间)")
},
})
wxTimer.start(this);
wxTimer.stop();

封装方法中因为用到page页的data,因此需要在调用start()的时候传入 this

在data中添加timer对象

data:{
timer: {
remaining: '00:00:00' }
}

在页面中就可以通过 timer.remaining 就可以显示倒计时

在调用wxTimer的时候,expired_at传入的值需要特别留意。

在小程序开发中,ios是个很头疼的事情,下面就“时间”来简单的说一下

  • 对于上面代码中提到的 2018-9-27 23:28:00.14756

    • 在ios中是不支持‘-’的,应该替换为 '/'

    • 还有就是在ios中不支持 ‘.’ ,所以应该将'.'之后的数字去掉

在方法中这些都已经实现了,如果想了解详细可以查看代码

参数说明:

  • expired_at:倒计时结束时间

  • complete:回调函数,倒计时结束后调用改方法

  • expired:回调函数,当传入的时间过期时调用该方法

马云地址:https://gitee.com/WoRuoYiRuFeng/wx_smallProgram_countDown

附件(wxTimer.js)

 var wxTimer = function (initObj) {
initObj = initObj || {};
this.complete = initObj.complete; //结束任务(方法)
this.expired = initObj.expired; //过期执行(方法) this.intervarID; //计时ID
this.expired_at = initObj.expired_at || "00:00:00"; //过期时间
} wxTimer.prototype = {
//开始
start: function (self) {
let that = this;
let expired_at = new Date(that.expired_at.replace(/-/g, "/")).getTime();
// expired_at = new Date(expired_at).getTime(); // ios下不执行,返回为null
let nowTime = new Date().getTime();
let remaining = (expired_at - nowTime); //计算剩余的毫秒数
// 过期
if (remaining < 0) {
if (that.expired) {
that.expired();
}
return
}
function begin() {
// 过期
// if (remaining < 0) {
// if (that.expired) {
// that.expired();
// }
// that.stop();
// }else{ let hours = parseInt(remaining / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
let minutes = parseInt(remaining / 1000 / 60 % 60, 10);//计算剩余的分钟
let seconds = parseInt(remaining / 1000 % 60, 10);//计算剩余的秒数
hours = checkTime(hours);
minutes = checkTime(minutes);
seconds = checkTime(seconds);
// 结束任务
if (hours <= 0 && minutes <= 0 && seconds <= 0) {
if (that.complete) {
that.complete();
}
that.stop();
}
if (hours >= 0 || minutes >= 0 || seconds >= 0) {
self.setData({
['timer.remaining']: hours + ":" + minutes + ":" + seconds
});
}
remaining = remaining - 1000;
// }
}
function checkTime(timer){
if (timer < 10){
timer = "0" + timer;
}
return timer;
}
// begin();
this.intervarID = setInterval(begin, 1000);
},
//结束
stop: function () {
clearInterval(this.intervarID);
}
} module.exports = wxTimer;

小程序实现倒计时:解决ios倒计时失效(setInterval失效)的更多相关文章

  1. 关于微信小程序开发环境苹果IOS真机预览报SSL协议错误问题解决方案

                              微信小程序开发环境苹果IOS真机预览报SSL协议错误问题 原文来自:https://blog.csdn.net/qq_27626333/articl ...

  2. 小程序clearinterval无效解决

    小程序clearinterval无效解决 小程序clearinterval清除定时器无效,原因是定时器使用与清除方法不对导致的,我们应将定时器绑定变量,这样在关闭页面清空定时器clearinterva ...

  3. 微信小程序+PHP:动态显示项目倒计时(格式:4天7小时58分钟39秒)

    1.一般我们说的显示秒杀都是指的单条数据,循环我没做. 效果: 2.wxml代码: <p class="endtime_act">距报名截止还有: <block ...

  4. 微信小程序【获取验证码】倒计时效果

    最近开始接触微信小程序,会记录一些相关的小功能——例如这次是点击[获取验证码]按钮出现的倒计时效果. 原文: http://blog.csdn.net/Wu_shuxuan/article/detai ...

  5. 关于小程序bindregionchange事件在IOS崩溃的问题

    先说下原因,我在bindregionchange事件触发函数中设置了经纬度,而latitude和longitude是绑定在map组件上的,滑动地图的过程中重新设置了地图中心点的经纬度,会导致地图本身的 ...

  6. 微信小程序单向数据流解决

    1. 小程序中没有vue中v-model一样的双向数据机制,并且小程序也不像vue那样的进行 实时的数据驱动视图. 小程序页面数据加载完成后再去改变data中的数据页面是 不会有变化的. 2. 解决: ...

  7. 小程序 ----踩坑 ---安卓iOS兼容等

    关于小程序一些小功能的代码都在这个GitHub上,感兴趣的可以去看看,https://github.com/huihuijiang/miniProgram目前有:列表左滑删除,拖拽浮标 一.小程序坑1 ...

  8. 微信小程序图片变形解决方法

    微信小程序的image标签中有个mode属性,使用aspectFill即可 注:image组件默认宽度300px.高度225px mode 有效值: mode 有 13 种模式,其中 4 种是缩放模式 ...

  9. 小程序使用wxs解决wxml保留2位小数问题

    1.出现溢出表现 从图中可以看到数字超出了很长长度.代码里面是如下这样的.为什么在0.35出现?或者一些相成的计算出现? 而 0.34却不会.(wap.0834jl.com) 0.41 也会出现,好像 ...

随机推荐

  1. bzoj 4475: [Jsoi2015]子集选取

    233,扒题解的时候偷瞄到这个题的题解了,,GG 暴力发现是2^(nm),然后就是sb题了 #include <bits/stdc++.h> #define LL long long us ...

  2. computed、methods、watch

    computed:计算属性将被混入到 Vue 实例中.所有 getter 和 setter 的 this 上下文自动地绑定为 Vue 实例. methods:methods 将被混入到 Vue 实例中 ...

  3. 「Luogu1901」发射站

    传送门 Luogu 解题思路 单调栈裸题,扫两遍处理出每个点左边第一个比他高的和右边第一个比他高的,然后模拟题意即可. 细节注意事项 咕咕咕. 参考代码 #include <algorithm& ...

  4. wdcp升级php5.8到php7.1.12后安装gitlab

    .安装依赖包 sudo yum install -y curl policycoreutils-python openssh-server cronie .使用清华大学开源软件镜像源安装 vim /e ...

  5. express写的接口在疯狂刷新几十次后,服务器挂掉

    用到的命令行: show status like 'Threads%'; show variables like '%max_connections%'; show global status lik ...

  6. struts2令牌(token)内部原理

      小菜最近接触了struts2中的令牌知识,由于该知识点比较重要,因此想弄明白些,于是满怀信心的上网查阅资料,结果让小菜很无奈,网上的资料千篇一律,总结出来就一句话:“访问页面时,在页面产生一个to ...

  7. Eclipse创建一个普通maven项目详细步骤

    首先找到Eclipse最顶部左边的File,new一个 Maven Project项目 下一步,勾选第二个即可 下一步,选择  maven-archetype-webapp Group Id 写域名倒 ...

  8. SciPy 特殊函数

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  9. HDU 3065 病毒侵袭持续中 (模板题)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  10. Python学习笔记之基础篇(二)python入门

    一.pycharm 的下载与安装: 使用教程:https://www.cnblogs.com/jin-xin/articles/9811379.html 破解的方法:http://xianchang. ...