倒计时 天 时 分 秒
dateformat: function (micro_second) {
//将 2112-07-07 19:45:00 格式 时间字符串 转化成秒数
var second = Date.parse(new Date(micro_second)) / 1000 - Date.parse(new Date()) / 1000;
// 总秒数
// var second = Math.floor(micro_second / 1000);
// 天数
var day = Math.floor(second / 3600 / 24);
// 总小时
var hr = Math.floor(second / 3600);
// 小时位
var hr2 = hr % 24;
// 分钟位
var min = Math.floor((second - hr * 3600) / 60);
// 秒位
var sec = (second - hr * 3600 - min * 60);// equal to => var sec = second % 60;
return day + "天" + hr2 + "时" + min + "分" + sec + "秒";
},
/* 秒杀倒计时 */
countdown() {
let that = this;
let time =this.data.len_time;
// 渲染倒计时时钟
var clock = this.dateformat(time)//格式化时间
that.setData({
clock: clock
});
if (time <= 0) {
that.setData({
clock: "活动结束"
});
// timeout则跳出递归
return false;
}
// settimeout实现倒计时效果 每秒运行一次
setTimeout(function () {
// 放在最后--
time -= 1;
that.countdown()
}, 1000)
},
data: {
len_time:'',
clock:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.countdown();
},
页面 展示
<view class="time">倒计时
{{clock}}
</view>