效果图
.wxml
<view wx:if="{{time.deadline > 0}}">
<view>单处理-转天时分秒
<view>{{time.down}}</view>
</view>
<view>单处理-转时分秒
<view>{{time.huly}}</view>
</view>
</view>
<view wx:for="{{array}}" wx:key="{{index}}" wx:if="{{item.deadline > 0}}">
<view>多处理-转天时分秒
<view>{{item.down}}</view>
</view>
<view>多处理-转时分秒
<view>{{item.huly}}</view>
</view>
</view>
.wxss
view{box-sizing: border-box;}
page>view{font-size: 26rpx;padding:40rpx 30rpx;display: flex;justify-content: space-between;line-height: 44rpx;
border-bottom: 1px solid #f4f4f4;}
page>view:nth-child(1){border-bottom: 1px solid #FFCCCC;}
.js
Page({
data: {
time:{deadline: "1289602000"},
array:[
{deadline: "1289602000"},
{deadline: "328960200"},
{deadline: "289602000"},
]
},
onUnload(e){
clearInterval(this.data.tiem_one)
clearInterval(this.data.tiem_two)
},
onHide(e){
clearInterval(this.data.tiem_one)
clearInterval(this.data.tiem_two)
},
onLoad(options){
this.single()
this.multiple()
},
single(e){//单处理
var that = this
var time = that.data.time,days,hours,minutes,seconds,hours_s
that.data.tiem_one = setInterval(function() {
time.deadline = time.deadline - 1000
days = parseInt(time.deadline / (1000 * 60 * 60 * 24));
hours = parseInt((time.deadline % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
minutes = parseInt((time.deadline % (1000 * 60 * 60)) / (1000 * 60));
seconds = parseInt((time.deadline % (1000 * 60)) / 1000);
if (time.deadline < 1) {
that.setData({
time:time
})
clearInterval(that.data.tiem_one)
return false
}
hours_s = (hours + days * 24) > 9 ? (hours + days * 24) : '0' + (hours + days * 24)
hours = hours > 9 ? hours : '0' + hours
minutes = minutes > 9 ? minutes : '0' + minutes
seconds = seconds > 9 ? seconds : '0' + seconds
time.down = days + '天' + hours + '时' + minutes + '分' + seconds + '秒'
time.huly = hours_s + '时' + minutes + '分' + seconds + '秒'
that.setData({
time:time
})
},1000)
},
multiple(e){//多处理
var that = this
var array = that.data.array,days,hours,minutes,seconds,hours_s
that.data.tiem_two = setInterval(function() {
array.forEach((item,index) => {
if(item.deadline > 0){
item.deadline = item.deadline - 1000
days = parseInt(item.deadline / (1000 * 60 * 60 * 24));
hours = parseInt((item.deadline % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
minutes = parseInt((item.deadline % (1000 * 60 * 60)) / (1000 * 60));
seconds = parseInt((item.deadline % (1000 * 60)) / 1000);
hours_s = (hours + days * 24) > 9 ? (hours + days * 24) : '0' + (hours + days * 24)
hours = hours > 9 ? hours : '0' + hours
minutes = minutes > 9 ? minutes : '0' + minutes
seconds = seconds > 9 ? seconds : '0' + seconds
item.down = days + '天' + hours + '时' + minutes + '分' + seconds + '秒'
item.huly = hours_s + '时' + minutes + '分' + seconds + '秒'
}
})
that.setData({
array:array
})
},1000)
},
})
遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。