防抖节流
<body style="height:2000px">
<input type="text">
<script>
let inp = document.querySelector("input");
inp.oninput = debounce(function() {
console.log(this.value);
}, 500)
function debounce(fn, time) {
let t = null;
return function() {
if (t !== null) {
clearTimeout(t);
}
t = setTimeout(() => {
fn.call(this);
}, time)
}
}
window.onscroll = throttle(function() {
console.log("hhhh");
}, 500)
function throttle(fn, time) {
let flag = true;
return function() {
if (flag) {
setTimeout(() => {
fn.call(this);
flag = true;
}, 500)
}
flag = false;
}
}
</script>
</body>