<body>
<input type="text">
<button>弹出</button>
<script>
var input = document.querySelector('input')
var btn = document.querySelector('button')
function debounce(func, wait) {
let timeout
return function () {
let context = this
let args = arguments
clearTimeout(timeout)
timeout = setTimeout(function () {
func.apply(context, args)
}, wait)
}
}
function throttled(fn, delay = 500) {
let timer = null
return function (...args) {
if (!timer) {
timer = setTimeout(() => {
fn.apply(this, args)
timer = null
}, delay);
}
}
}
btn.addEventListener('click', debounce(clickHandler, 3000))
function clickHandler() {
let ret = fetch('/data.json')
ret.then(res => res.json().then(res => console.log(res)))
}
input.addEventListener("input", throttled(inputHandler, 0))
function inputHandler(e) {
console.log(e.target.value)
}
</script>
</body>