Skip to content

Instantly share code, notes, and snippets.

@Lxxyx
Created October 31, 2016 03:41
Show Gist options
  • Save Lxxyx/b54853973e898a8789498bf8c25a385e to your computer and use it in GitHub Desktop.
Save Lxxyx/b54853973e898a8789498bf8c25a385e to your computer and use it in GitHub Desktop.
debuonce
// 1000内,只触发一次
const throttle = (cb, time = 1000, ctx = null) => {
let timer = null
return (...args) => {
if (timer) {
return
}
timer = setTimeout(() => {
cb.apply(ctx, args)
timer = null
}, time)
}
}
document.body.addEventListener('mousemove', throttle((e) => {
console.log(e)
}, 1000), false)
// 不管多少次,总是推迟到1000ms后触发
const debounce = (cb, time = 1000, ctx = null) => {
let timer = null
return (...args) => {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
cb.apply(ctx, args)
}, time)
}
}
document.body.addEventListener('mousemove', debounce((e) => {
console.log(e)
}, 1000), false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment