之前幫一個群員解決倒計時的問題,分享過來給大家看看
1、讓數字進行倒計時
2、讓多個數字進行倒計時
3、讓一個數組倒計時
4、優化倒計時的方法
5、把數字換成HH:ss的格式
這里我就直接上解決的代碼
<view>
<view wx:for="{{total_micro_second}}">
<view class='row'>
<view>{{total_micro_second[index].t}}</view>
<view data-index='{{index}}' bindtap='timeZT'>{{item.ztText}}</view>
<view data-index='{{index}}' bindtap='timeStop'>{{item.tzText}}</view>
</view>
</view>
</view>
Page({
/**
* 頁面的初始數據
*/
data: {
total_micro_second: []
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
var that = this
var time = this.data.total_micro_second
var total_micro_second = "00:29"; //表示60秒倒計時,想要變長就把60修改更大
var total_micro_second2 = "00:35";
var date = {}
date.t = total_micro_second
date.f = true
date.ztText = "暫停"
date.tzText = "停止"
time.push(date)
var date2 = {}
date2.t = total_micro_second2
date2.f = true
date2.ztText = "暫停"
date2.tzText = "停止"
time.push(date2)
console.log("入前", time)
//這里是組裝數據,也可以直接在data里面寫死,實際情況是根據查詢的結果進行生成
this.setData({
total_micro_second: time
})
setTimeout(function () {
for (var i in time) {
count_down(that, i);
}
}, 1000)
},
//時間暫停
timeZT: function (e) {
var index = e.currentTarget.dataset.index
var times = this.data.total_micro_second
console.log("暫停", index)
console.log("暫停的數據", times[index])
console.log(times[index].t)
if (times[index].t != "00:00") {
if (times[index].f) {
times[index].f = false
times[index].ztText = "繼續"
} else {
times[index].f = true
times[index].ztText = "暫停"
}
this.setData({
total_micro_second: times
})
}
},
timeStop: function (e) {
var index = e.currentTarget.dataset.index
var times = this.data.total_micro_second
console.log("停止", index)
console.log("停止的數據", times[index])
if (times[index].f) {
times[index].f = false
times[index].tzText = "已停止"
times[index].t = "00:00"
}
// else {
// times[index].f = true
// times[index].t = "00:20"
// times[index].tzText = "停止"
// }
this.setData({
total_micro_second: times
})
}
})
function count_down(that, i) {
var times = that.data.total_micro_second
console.log(i, times)
if (times[i].f) { //為true
var tim = times[i].t.split(":");
var second = parseInt(tim[0]) * 60 //分鐘
second += parseInt(tim[1]) //秒
//console.log("總時間", second)
if (second <= 0) { //可能存在2秒內的誤差
times[i].t = "00:00"
times[i].f = false
that.setData({
total_micro_second: times
});
i = "S"
return;
}
second -= 1
var min = parseInt(second / 60)
var sec = second - parseInt(second / 60) * 60
times[i].t = fill_zero_prefix(min) + ":" + fill_zero_prefix(sec)
that.setData({
total_micro_second: times
});
}
// // 放在最后--
if (i != "S") {
setTimeout(function () {
count_down(that, i);
}, 1000)
}
}
// 位數不足補零
function fill_zero_prefix(num) {
return num < 10 ? "0" + num : num
}
/**
* 下劃線
*/
//紅色下劃線
.U_red {
padding-bottom: 2rpx;app.wxss樣式(我所有項目都會有這個)
border-bottom: 1px solid red;
}
//黑色下劃線
.U_black {
padding-bottom: 2rpx;
border-bottom: 1px solid black;
}
//黑色上劃線
.U_black_top {
padding-bottom: 2rpx;
border-top: 1px solid black;
}
//白色下劃線
.U_white {
padding-bottom: 2rpx;
border-bottom: 1px solid white;
}
/**
* Test class
*/
//黑色邊框
.border_black {
border: 1rpx solid black;
}
//紅色邊框
.border_red {
border: 1rpx solid red;
}
//白色圓角
.border_white {
border: 1rpx solid white;
border-radius: 30rpx;
}
//布局
.row {
display: flex;
flex-direction: row;
justify-content: space-between;
}
//布局
.row_c {
display: flex;
flex-direction: row;
justify-content: center;
}
//布局
.row_content {
display: flex;
flex-direction: row;
justify-content: flex-start;
}
//布局
.column {
display: flex;
flex-direction: column;
justify-content: space-between;
}
//黑色
.bk {
color: black;
}
//字體加粗
.b_font {
font-weight: 600;
}
tip:
1、如有遇到新問題,可以在下方留言或者加QQ群437729329 進行咨詢
2、如果有優化的童鞋可以在下方留言
3、如果有封裝好的,也可以在下方留言