Skip to content

Instantly share code, notes, and snippets.

@Alex-xd
Last active April 27, 2017 03:16
Show Gist options
  • Save Alex-xd/eaafda842d602b89147d6108e2576d4b to your computer and use it in GitHub Desktop.
Save Alex-xd/eaafda842d602b89147d6108e2576d4b to your computer and use it in GitHub Desktop.
function debounce(dom, event, fn, timeout) {
var timeout = timeout || 3; // 默认延迟3s内点击无效
var timer = null;
// cornor cases
if (!dom || !event || !(event in dom) || typeof fn !== 'function') {
return
}
dom.addEventListener(event, function () {
var _this = this;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(_this)
}, timeout)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment