Open Mardanjan opened 4 years ago
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>防抖</title>
</head>
<body>
<input type="text" name="" id="input">
</body>
<script>
var input = document.querySelector('#input')
input.oninput = function() {
debounce()
}
var timer
function debounce () {
clearTimeout(timer)
timer = setTimeout( () => {
console.log(input.value)
}, 500)
}
</script>
</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" name="" id="input">
</body>
<script>
var input = document.querySelector('#input')
var timer = true
input.oninput = function () {
if (!timer) {return}
timer = false
setTimeout(() => {
console.log('执行了')
timer = true
}, 200);
}
</script>
</html>
节流概念 Throttle
防抖的概念 Debounce
节流实现
防抖实现
节流和防抖的伪理论