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

第 103 期(内功-性能):防抖 #106

Open
wingmeng opened this issue Sep 5, 2019 · 0 comments
Open

第 103 期(内功-性能):防抖 #106

wingmeng opened this issue Sep 5, 2019 · 0 comments

Comments

@wingmeng
Copy link
Collaborator

wingmeng commented Sep 5, 2019

防抖和节流都是我们解决高频触发事件的两种常用解决方案,与节流不同,防抖是将若干次事件调用合成为一次,并在给定时间过去之后,或者连续事件完全触发完成之后,调用一次。

防抖函数:

function debounce(fn, delay) {
  var timer = null;
  delay = delay || 1000;

  return function() {
    var context = this;
    var args = arguments;
    
    clearTimeout(timer);
    timer = setTimeout(function() {
      fn.apply(context, args);
    }, delay);
  }
}

应用举例:

inputElm.addEventListener('input', debounce(function() {
  console.log(Date.now())
}));
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