优化 ~ 防抖与节流

一:函数的节流: 一定时间内只触发一次 

可以是直接给一个定时器,按时触发,

// 函数节流 var canRun = true; document.getElementById("throttle").

onscroll = function(){

if(!canRun){ // 判断是否已空闲,如果在执行中,则直接return return;

}

canRun = false;

setTimeout(function(){

console.log("函数节流"); canRun = true;
}, 300); };

也可以根据一个条件判断触发

  二:函数防抖 

在规定的时间内只触发一次,如果多次触发,清除上次触发的事件,重新计算时间,

百度搜索就是防抖    http://demo.deanhan.cn/suggestion.html

猜你喜欢

转载自www.cnblogs.com/tiangeng/p/10097943.html