前端防抖,double click 克星

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>防抖</title>
</head>
<body>
<button id="debounce">防抖,防抖,防抖</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>

  

原文地址:https://www.cnblogs.com/xiufengd/p/14149275.html