您的位置:首页 > 移动开发 > 微信开发

利用函数防抖和函数节流提高小程序性能

2019-01-14 18:27 232 查看

函数防抖

const _.debounce = (func, wait) => {

  
let timer;

 

  
return
 
() => {

    
clearTimeout(timer);

    
timer = setTimeout(func, wait);

  
};

};

 

        函数节流

 

const throttle = (func, wait) => {

  
let last = 0;

  
return
 
() => {

    
const current_time = +
new
 
Date();

    
if
 
(current_time - last > wait) {

      
func.apply(
this
, arguments);

      
last = +
new
 
Date();

    
}

  
};

};

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: