数组展平 --递归
- [ [1, [2, 3, [4,5] ] ] ] ===> [1,2,3,4,5]
function flatten(arr) {
return [].concat(
...arr.map(x => {
return Array.isArray(x) ? flatten(x) : x
})
)
}
console.log( flatten([ [1, [2, 3, [4,5] ] ] ]));
节流的–递归
- 固定时间内只允许执行一次
document.addEventListener('scroll', throttle(console.log)) // 监听页面滚动
function throttle(func, delay = 2000){
let lock = false
return (...args) => {
if(lock){return} // 当 lock == true 直接返回
func(...args)
lock = true
setTimeout(() => {lock = false}, delay) // 2s后 锁再打开
}
}