<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>防抖</title>
</head>
<body>
<button class="dianji">点击</button>
</body>
<script>
// 防抖函数
var timer; // 维护同一个timer
function debounce(fn, delay) {
clearTimeout(timer);
timer = setTimeout(function () {
fn();
}, delay);
}
let dianji = document.querySelector(".dianji");
dianji.addEventListener("click", function () {
debounce(function () {
console.log("点击了");
}, 1000);
});
</script>
</html>
简单的防抖
最新推荐文章于 2024-08-22 14:32:34 发布