Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 防抖
- * @param {Function} func 执行的函数
- * @param {Number} wait 防抖的时间
- * @param {Boolean} immediate 是否为立即执行
- */
- function debounce(func, wait, immediate) {
- let time = null;
- return function() {
- let _this = this;
- let args = arguments;
- if (time) clearTimeout(time);
- if (immediate) {
- let callNow = !time;
- setTimeout(() => {
- time = null;
- }, wait);
- if (callNow) func.apply(_this, args);
- } else {
- time = setTimeout(() => {
- func.apply(_this, args);
- }, wait);
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement