Created
October 31, 2016 03:41
-
-
Save Lxxyx/b54853973e898a8789498bf8c25a385e to your computer and use it in GitHub Desktop.
debuonce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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