【JavaScript】节流防抖

前言

参考文档:https://www.cnblogs.com/momo798/p/9177767.html

代码实现

  • 节流

把这块代码复制到流程中即可。
这个虽然简单,但是好像是全局的?不清楚

this.time && clearTimeout(this.time)
this.time = setTimeout(() => {
    
    
   // 业务代码
   this._likeusername(val)
 }, 1200)
  • 防抖
// 防抖
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));

猜你喜欢

转载自blog.csdn.net/s1441101265/article/details/107553642