关于html:第-47-题什么是防抖和节流

2次阅读

共计 621 个字符,预计需要花费 2 分钟才能阅读完成。

防抖

触发高频事件后 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);
            }
        }
    }
};

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

  • 【继续保护 / 更新 500+ 前端面试题 / 笔记】https://github.com/noxussj/In…
  • 【大数据可视化图表插件】https://www.npmjs.com/package…
  • 【利用 THREE.JS 实现 3D 城市建模(珠海市)】https://3d.noxussj.top/
正文完
 0