87,917
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="text" />
</body>
const input = document.querySelector("input")
let time
input.oninput = () => {
if (time) {
clearTimeout(time)
}
time = setTimeout(() => {
console.log(input.value)
}, 2000)
}
function debounce(fn, wait) {
let time
return function () {
if (time) {
clearTimeout(time)
}
time = setTimeout(function () {
fn()
}, wait)
}
}
function fn() {
console.log(input.value)
}
input.oninput = debounce(fn, 2000)