美文网首页
vue列表秒杀倒计时

vue列表秒杀倒计时

作者: 白小纯Zzz | 来源:发表于2024-03-11 13:55 被阅读0次

    使用组件:

    <count-down :dateTime="taskDueTime"></count-down>   
    

    组件:

    <template>
    <view class="timeBox" v-if="countdownShow">
        <text class="value" v-if="pageData.day > 0">{{ pageData.day }}</text><text class="label"
            v-if="pageData.day > 0">天</text>
        <text class="value">{{ pageData.hour }}</text><text class="label">:</text>
        <text class="value">{{ pageData.minute }}</text><text class="label">:</text>
        <text class="value">{{ pageData.second }}</text>
    </view>
    </template>
    <script>
    export default {
        name: "count-down",
        data() {
            return {
                countdownShow: true,
                pageData: {
                    day: '0',
                    hour: '00',
                    minute: '00',
                    second: '00'
                }
            };
        },
        props: {
            dateTime: {
                type: String
            }
        },
        created() {
            this.interval = setInterval(() => {
                this.getCountdown()
            }, 1000);
        },
        beforeDestroy() {
            clearInterval(this.interval)
        },
        methods: {
            getCountdown() {
                const endTime = new Date(this.dateTime).getTime() / 1000 - (new Date() / 1000 | 0)
                if (endTime <= 0) {
                    clearInterval(this.interval)
                    this.countdownShow = false
                    this.$emit('countdownClose');
                }
                let date = {
                    day: Math.floor(endTime / 86400),
                    hour: Math.floor(endTime % 86400 / 3600),
                    minute: Math.floor(endTime % 3600 / 60),
                    second: Math.floor(endTime % 3600 % 60)
                }
    
                Object.keys(date).forEach(value => {
                    if (date[value].toString().length < 2 && value !== 'day') {
                        this.pageData[value] = "0" + date[value]
                    } else {
                        this.pageData[value] = date[value]
                    }
                })
            }
        }
    }
    </script>
    

    相关文章

      网友评论

          本文标题:vue列表秒杀倒计时

          本文链接:https://www.haomeiwen.com/subject/otpmzdtx.html