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

防抖和节流 #3

Open
fish519 opened this issue May 5, 2019 · 1 comment
Open

防抖和节流 #3

fish519 opened this issue May 5, 2019 · 1 comment

Comments

@fish519
Copy link
Owner

fish519 commented May 5, 2019

确保最后的能执行,flush

@fish519
Copy link
Owner Author

fish519 commented May 5, 2019

const debounce = (fn, time) => {
  let timeout = null;
  return () => {
    clearTimeout(timeout);
    timeout = setTimeout(function() {
      fn.apply(this, arguments)
    }, time)
  }
}

n秒内执行最后一次

const throttle = (fn, time) => {
  let canRun = true;
  return function(){
    if(!canRun) return;
    fn.apply(this, arguments);
    canRun = false;
    setTimeout(() => {
      canRun = true;
    }, time);
  }
}

n秒内执行一次

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant