本文有3个倒计时功能的Demo:
1.时间戳转换成时间格式倒计时
2.倒计时60秒
3.时间格式的数据直接开始倒计时
直接上代码了:
1.时间戳转换成时间格式倒计时
<text>{{hhh}}:{{mmm}}:{{sss}}</text>
Page({
// 倒计时
count_down: function (countDown_time) {
var that = this;
var time = countDown_time.split(':')
var hhh = parseInt(time[0])
var mmm = parseInt(time[1])
var sss = parseInt(time[2])
this.setData({
sss: (sss < 10) ? '0' + sss : sss,
mmm: (mmm < 10) ? '0' + mmm : mmm,
hhh: (hhh < 10) ? '0' + hhh : hhh
})
var Interval = setInterval(function () {
if (sss > 0) {
sss--
} else {
if (sss && mmm && hhh == 0) {
clearInterval(Interval)
console.log('时间到')
}
}
}
if (sss == 0) {
if (mmm > 0) {
mmm--
sss = 59;
}
if (mmm == 0 && hhh > 0) {
hhh--
sss = 59;
mmm = 59;
}
}
that.setData({
sss: (sss < 10) ? '0' + sss : sss,
mmm: (mmm < 10) ? '0' + mmm : mmm,
hhh: (hhh < 10) ? '0' + hhh : hhh
})
}, 1000)
},
// 时间戳转换成时分秒
timestampToTime: function(timestamp) {
var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
var m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return h + m + s;
},
onLoad: function (options) {
var countDown_time = new Date().getTime();
this.count_down(this.timestampToTime(countDown_time))
}
})
2.倒计时60秒:
<view>{{time}}</view>
var test_number = 60
Page({
count_down: function () {
var _this = this;
setInterval(function () {
test_number--
if (test_number >= 0) {
_this.setData({
time: test_number
})
}else{
console.log('我已经到0了')
}
}, 1000)
},
onShow: function () {
this.count_down()
}
})
3.时间格式实现倒计时
<text>{{hhh}}:{{mmm}}:{{sss}}</text>
Page({
onLoad:function(){
var end_time = '06:22:10';
this.count_down(end_time);
},
// 倒计时
count_down: function (countDown_time) {
var that = this;
var time = countDown_time.split(':')
var hhh = parseInt(time[0])
var mmm = parseInt(time[1])
var sss = parseInt(time[2])
this.setData({
sss: (sss < 10) ? '0' + sss : sss,
mmm: (mmm < 10) ? '0' + mmm : mmm,
hhh: (hhh < 10) ? '0' + hhh : hhh
})
var Interval = setInterval(function () {
if (sss > 0) {
sss--
} else {
if (sss && mmm && hhh == 0) {
clearInterval(Interval)
console.log('时间到')
}
}
}
if (sss == 0) {
if (mmm > 0) {
mmm--
sss = 59;
}
if (mmm == 0 && hhh > 0) {
hhh--
sss = 59;
mmm = 59;
}
}
that.setData({
sss: (sss < 10) ? '0' + sss : sss,
mmm: (mmm < 10) ? '0' + mmm : mmm,
hhh: (hhh < 10) ? '0' + hhh : hhh
})
}, 1000)
}
})
毫秒转成时分秒倒计时
count_down(84557095);
// 倒计时
count_down: function(countDown_time) {
var that = this;
var days = parseInt(countDown_time / (1000 * 60 * 60 * 24));
var hours = parseInt((countDown_time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt((countDown_time % (1000 * 60 * 60)) / (1000 * 60));
var seconds = (countDown_time % (1000 * 60)) / 1000;
var aaaa = hours + ":" + minutes + ":" + seconds;
var time = aaaa.split(':')
var hhh = parseInt(time[0])
var mmm = parseInt(time[1])
var sss = parseInt(time[2])
this.setData({
sss: (sss < 10) ? '0' + sss : sss,
mmm: (mmm < 10) ? '0' + mmm : mmm,
hhh: (hhh < 10) ? '0' + hhh : hhh
})
var Interval = setInterval(function() {
if (sss > 0) {
sss--
} else {
if (sss && mmm && hhh == 0) {
clearInterval(Interval)
console.log('时间到')
}
}
if (sss == 0) {
if (mmm > 0) {
mmm--
sss = 59;
}
if (mmm == 0 && hhh > 0) {
hhh--
sss = 59;
mmm = 59;
}
}
that.setData({
sss: (sss < 10) ? '0' + sss : sss,
mmm: (mmm < 10) ? '0' + mmm : mmm,
hhh: (hhh < 10) ? '0' + hhh : hhh
})
},
1000)
},