函数节流
一段时间执行一次之后,就不执行第二次
//函数节流function throttle(fn, delay) {let canUse = truereturn function () {if (canUse) {canUse = falseconst context = thisconst args = argumentssetTimeout(() => {fn.apply(context, args)canUse = true}, delay)}}}
函数防抖
在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时。
//函数防抖function debounce(fn, delay) {let timer = nullreturn function () {const context = thisconst args = argumentsif (timer) {clearTimeout(timer)}timer = setTimeout(() => {fn.apply(context, args)}, delay)}}
