美文网首页
倒计时组件

倒计时组件

作者: McDu | 来源:发表于2020-11-03 11:39 被阅读0次
<!--倒计时
使用说明:传入time(时间差 秒)
-->
<template>
    <div class="countdown">
        <slot v-if="format" :time="formatCountdown(timeDiff)"></slot>
        <slot v-else :time="timeDiff"></slot>
    </div>
</template>
<script>
export default {
    props: {
        // 时间差
        time: {
            type: Number,
            default: 60
        },
        // 步长
        step: {
            type: Number,
            default: 1
        },
        // 开关
        switch: {
            type: Boolean,
            default: null
        },
        // 格式化
        format: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            // 时间函数
            timer: null,
            // 倒计时
            timeDiff: 0
        };
    },
    computed: {
        // 步长转换为毫秒
        computedStep() {
            return this.step * 1000;
        }
    },
    watch: {
        // 监听倒计时时间差变化
        time: {
            handler(val) {
                this.timeDiff = val;
                const isSwitch = typeof this.switch === 'boolean';
                if(val && !this.timer) {
                    if(isSwitch) {
                        if(this.switch) {
                            this.triggerTimer();
                        } else {
                            this.clearTimer();
                        }
                    } else {
                        this.triggerTimer();
                    }
                } else {
                    this.clearTimer();
                }
            },
            immediate: true
        },
        // 监听开关变化
        switch: {
            handler(val) {
                if(val) {
                    this.triggerTimer();
                } else {
                    this.clearTimer();
                }
            }
        }
    },
    methods: {
        // 触发倒计时函数
        triggerTimer() {
            this.timer = setTimeout(() => {
                this.timeDiff--;
                if(this.timeDiff <= 0) {
                    this.$emit('on-end');
                } else {
                    this.$emit('on-countdown', this.timeDiff);
                    this.triggerTimer();
                }
            }, this.computedStep);
        },
        // 清除定时器
        clearTimer() {
            if(this.timer) {
                clearTimeout(this.timer);
            }
        },
        // 格式化时间戳
        formatCountdown(timeDiff) {
            // 获取还剩多少小时
            const hour = parseInt((timeDiff / 60 / 60).toString());

            // 获取还剩多少分钟
            let minute;
            if(this.format.includes('hh') || this.format.includes('HH')) {
                minute = parseInt(((timeDiff / 60) % 60).toString());
            } else {
                minute = parseInt((timeDiff / 60).toString());
            }

            // 获取还剩多少秒
            let second;
            if(this.format.includes('mm') || this.format.includes('MM')) {
                second = timeDiff % 60;
            } else {
                second = timeDiff;
            }

            let result = this.format;
            result = result.replace(/(hh|HH)/g, this.paddingZero(hour));
            result = result.replace(/(mm|MM)/g, this.paddingZero(minute));
            result = result.replace(/(ss|SS)/g, this.paddingZero(second));
            return result;
        },
        // 补零
        paddingZero(val) {
            if(val <= 0) {
                return '00';
            } else if(val < 10) {
                return `0${val}`;
            } else {
                return val.toString();
            }
        },
        // 重新倒计时
        reCountdown() {
            this.timeDiff = this.time;
        }
    },
    beforeDestroy() {
        this.clearTimer();
    }
};
</script>
<style lang="scss" scoped>
.countdown {
    display: inline-block;
}
</style>

相关文章

  • [小程序][医美]

    组件: 小程序组件开发模板 navbar 顶部导航组件封装原则 倒计时

  • React Native 倒计时组件

    功能: 实现倒计时组件,倒计时结束可以执行方法 1、CountDown.js import React, {Com...

  • react-native 倒计时 后台计时器继续走

    一个倒计时组件 优点 1:程序进入后台,继续执行倒计时2:跳转其他页面继续倒计时 直接上代码

  • canvas环形倒计时组件

    效果如下图一: Canvas环形倒计时组件 Canvas环形倒计时是基于Canvas实现的倒计时,建议于移动端使用...

  • vue倒计时组件

    模仿vantUI的倒计时组件,自己写了个简易的 使用方法:

  • 倒计时组件

    前言 运营类活动的交互组件规范编写结合实际设计需要,首先从行为心理学、动机理论的角度出发,论述组件对用户行为的影响...

  • 倒计时组件

  • 实现一个基于Vue的数字翻牌滚动组件

    数字翻牌组件在很多地方都有用到,比如倒计时、在线人数、销售量等等。这种组件可以封装一下,做成一个公用组件,这样在每...

  • vue中常见问题

    1.在一个页面声明一个定时器的做倒计时的时候,跳转到下一个组件页面,正常这时候组件被销毁,到达B组件的时候A组件已...

  • vue中常见问题

    1.在一个页面声明一个定时器的做倒计时的时候,跳转到下一个组件页面,正常这时候组件被销毁,到达B组件的时候A组件已...

网友评论

      本文标题:倒计时组件

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