前言
JS常见面试编程题
1.防抖
function debounce(fn, delay) {
let timer = null;
return function(...args) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(()=>{
timer = null;
fn.apply(this, args);
}, delay)
}
}
2.节流
function throttle(fn, interval) {
let lastTime;
let timer;
return function(...args) {
const now = Date.now();
if(lastTime && now - lastTime < interval) {
clearTimeout(timer);
timer = setTimeout(() => {
last = now;
fn.apply(this, args)
}, interval)
}
else {
last = now;
fn.apply(this, args)
}
}
}
3.数组去重
function deDuplicated(arr) {
// return Array.from(new Set(arr));
return [...new Set(arr)];
}
function deDuplicated2(arr) {
const result = [];
arr.forEach