提交 320a7a69 编写于 作者: H hulinneil

解决倒计时组件及示例时间不会重置的问题

上级 f10aefa4
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<view class="grace-countdown-numbers" :style="{borderColor:borderColor, color:fontColor, background:bgrColor}">{{s}}</view> <view class="grace-countdown-numbers" :style="{borderColor:borderColor, color:fontColor, background:bgrColor}">{{s}}</view>
</view> </view>
</template> </template>
<script> <script>
export default { export default {
name: "graceCountd", name: "graceCountd",
props: { props: {
...@@ -33,7 +33,8 @@ export default { ...@@ -33,7 +33,8 @@ export default {
} }
}, },
data() { data() {
return { return {
setTime:null,
h: '00', h: '00',
i: '00', i: '00',
s: '00', s: '00',
...@@ -62,9 +63,12 @@ export default { ...@@ -62,9 +63,12 @@ export default {
this.setInterValFunc(this); this.setInterValFunc(this);
} }
}, },
beforeDestroy(){
clearInterval(this.setTime)
},
methods: { methods: {
setInterValFunc:function(obj){ setInterValFunc:function(obj){
setInterval(function(){ obj.countDown(obj);}, 1000); this.setTime = setInterval(function(){ obj.countDown(obj);}, 1000);
}, },
countDown: function (self){ countDown: function (self){
var leftTime = self.leftTime - new Date(); var leftTime = self.leftTime - new Date();
......
...@@ -17,46 +17,73 @@ ...@@ -17,46 +17,73 @@
</template> </template>
<script> <script>
import graceCountd from "../../../components/graceCountd.vue"; import graceCountd from "../../../components/graceCountd.vue";
//模拟3个时间
var dateObj = new Date();
var currentTime = dateObj.getTime();
var timer1 = formatDateTime((currentTime + 1000 * 2000));
var timer2 = formatDateTime((currentTime + 1000 * 3000));
var timer3 = formatDateTime((currentTime + 1000 * 5000));
//时间戳 转 YY-mm-dd HH:ii:ss
function formatDateTime(inputTime){
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};
export default { export default {
data(){ data() {
return { var dateObj = new Date();
title : '倒计时', var currentTime = dateObj.getTime();
timer1 : timer1, var timer1 = this.formatDateTime((currentTime + 1000 * 2000));
timer2 : timer2, var timer2 = this.formatDateTime((currentTime + 1000 * 3000));
timer3 : timer3 var timer3 = this.formatDateTime((currentTime + 1000 * 5000));
} return {
}, title: '倒计时',
components:{ timer1: timer1,
graceCountd timer2: timer2,
timer3: timer3
}
},
components: {
graceCountd
},
methods: {
formatDateTime(inputTime) { //时间戳 转 YY-mm-dd HH:ii:ss
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}
},
} }
}
</script> </script>
<style> <style>
view{display:flex; width:100%; flex-wrap:wrap; justify-content:center;} view {
.uni-countdown{padding:2rpx 0; flex-wrap:nowrap; justify-content:center;} display: flex;
.uni-countdown-splitor{width:auto !important; justify-content:center; line-height:44upx; padding:0 5upx;} width: 100%;
.uni-countdown-numbers{line-height:44upx; width:auto !important; padding:0 10upx; justify-content:center; height:44upx; border-radius:8upx; margin:0 5upx; border:1px solid #000000; font-size:22upx;} flex-wrap: wrap;
</style> justify-content: center;
\ No newline at end of file }
.uni-countdown {
padding: 2rpx 0;
flex-wrap: nowrap;
justify-content: center;
}
.uni-countdown-splitor {
width: auto !important;
justify-content: center;
line-height: 44upx;
padding: 0 5upx;
}
.uni-countdown-numbers {
line-height: 44upx;
width: auto !important;
padding: 0 10upx;
justify-content: center;
height: 44upx;
border-radius: 8upx;
margin: 0 5upx;
border: 1px solid #000000;
font-size: 22upx;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册