Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

防抖和节流工具函数 #13

Open
Corle-He opened this issue May 10, 2019 · 0 comments
Open

防抖和节流工具函数 #13

Corle-He opened this issue May 10, 2019 · 0 comments

Comments

@Corle-He
Copy link
Owner

Corle-He commented May 10, 2019

他们都是可以防止一个函数被无意义的高频率调用

函数节流:是确保函数特定的时间内至多执行一次。
函数防抖:是函数在特定的时间内不再被调用后又重复执行。

函数防抖

let timer = null;
function fun(){
  console.log('onresize')
}
function throttle(method){
    return function(){
        if(timer !== null){
            clearTimeout(timer);
        }
        timer = setTimeout(fn,500);
    }
}

window.onresize = ()=>throttle(fun)

函数节流

// 函数节流
var canRun = true;
document.getElementById("throttle").onscroll = function(){
if(!canRun){
  return
}
canRun = false
setTimeout( function () {
    console.log("函数节流")
    canRun = true
  }, 500)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant