<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>防抖</title>
</head>
<body>
<button >防抖,防抖,防抖</button>
<script>
    window.onload = function () {
        let obtn = document.getElementById('debounce'); //获取按钮
        obtn.addEventListener('click',debounceHandle(debounce),false); //监听绑定事件
    }

    //防抖函数
    function debounceHandle (fn) {
        let timer = null;
        return function () {
            clearTimeout(timer); //如果存在事件就清除定时器
            timer = setTimeout(function(){ //如果不存在那么就开启定时器
                fn(this);
            },300)
        }
    }

    //执行函数
    function debounce() {
        console.log('防抖,防抖,防抖');
        //这里写要执行的代码
    }
</script>
</body>
</html>

  

相关文章:

  • 2021-05-30
  • 2022-01-11
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2021-06-01
猜你喜欢
  • 2022-12-23
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-24
  • 2021-11-22
相关资源
相似解决方案