倒计时组件:
<template>
<view class='container' >{{couent}}</view>
</template>
<script>
import wepy from 'wepy';
export default class Daojishi extends wepy.component {
props = {
djstime: {
type: Number,
default: 'null',
twoWay: true
}
};
data = {
couent: ''
};
countdown(num){
let that = this;
that.couent = that.dateformat(num)
that.$apply()
console.log(that.dateformat(num))
if (num <= 0) {
that.couent = '已经截止'
return
}
setTimeout(function() {
let newnum = num-1
that.countdown(newnum)
}, 1000)
}
dateformat(microSecond) {
var second = Math.floor(microSecond)
var hr = this.fillZeroPrefix(Math.floor(second / 3600))
var min = this.fillZeroPrefix(Math.floor((second - hr * 3600) / 60))
var sec = this.fillZeroPrefix((second - hr * 3600 - min * 60))// equal to => var sec = second % 60;
return hr + ':' + min + ':' + sec
}
fillZeroPrefix(num) {
return num < 10 ? '0' + num : num
}
onLoad() {
var num = this.$data.djstime
console.log(num)
var that = this;
this.countdown(num)
};
onHide(){
}
onShow() { };
}
</script>
组件引用:
<template>
<view class="container">
<Djs :djstime.sync="clock1"></Djs>
</view>
</template>
<script>
import wepy from 'wepy'
import djs from '../../components/daojishi'
export default class Time extends wepy.page {
config = {
navigationBarTitleText: '倒计时'
}
components = {
Djs: djs,
}
onLoad() {}
data = {
clock1: 3600,
}
}
</script>