前言
Reference document: https://www.cnblogs.com/momo798/p/9177767.html
代码实现
- Throttling
Just copy this piece of code into the process.
Although this is simple, it seems to be global? Not clear
this.time && clearTimeout(this.time)
this.time = setTimeout(() => {
// 业务代码
this._likeusername(val)
}, 1200)
- Anti-shake
// 防抖
function debounce(fn, wait) {
var timeout = null;
return function() {
if(timeout !== null) clearTimeout(timeout);
timeout = setTimeout(fn, wait);
}
}
// 处理函数
function handle() {
console.log(Math.random());
}
// 滚动事件
window.addEventListener('scroll', debounce(handle, 1000));