解决定时器叠加问题

let demo = new Vue({
    el: '#app',
    data: {
        msg: '我是跑马灯',
        timer: null,
    },
    methods: {
        start: function () {
            if (this.timer) {
                return;
            }
            this.timer = setInterval(() => {
                let start = this.msg.substring(0, 1);
                let end = this.msg.substring(1);
                this.msg = end + start;
            }, 500)
        },
        end: function () {
           //清除定时器后将timer置为0来叠加
            clearInterval(this.timer);
            this.timer = null;
        }
    }
});
发布了39 篇原创文章 · 获赞 2 · 访问量 4035

猜你喜欢

转载自blog.csdn.net/qq_43137725/article/details/103589046