防抖

触发高频事件后 n 秒内函数只会执行一次,如果 n 秒内高频事件再次被触发,则重新计算时间

<script>
export default {
    data() {
        return {
            timer: null
        };
    },
    methods: {
        click() {
            clearTimeout(this.timer);

            this.timer = setTimeout(() => {
                console.log('鼠标单击');
            }, 200);
        }
    }
};
</script>

节流

在单位时间内, 只会触发一次事件,如果事件触发后,又重复触发了同一事件,则忽略后面触发的事件,直到第一次事件的计时结束

<script>
export default {
    data() {
        return {
            isFinshed: true
        };
    },
    methods: {
        click() {
            if (this.isFinshed === true) {
                this.isFinshed = false;

                this.timer = setTimeout(() => {
                    console.log('鼠标单击');
                    this.isFinshed = true;
                }, 200);
            }
        }
    }
};

文章的内容/灵感都从下方内容中借鉴

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/noxussj/p/15269151.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程