防止频繁点击按钮

时间:2021-08-20 17:02:25
<html>

<head>

</head>

<body>
    <button id="btn">测试</button>
</body>

</html>
<script>
    FrequentClickHandle("btn",function(){
        console.log(new Date().toLocaleString());
    },2000);
    function FrequentClickHandle(domId, callback,timeoutTime) {
        var button = {
            enable: true,
            click: function (callback) {
                if (this.enable == true) {
                    this.enable = false;
                    callback();
                    setTimeout(active, timeoutTime);
                }
            }
        }
        var elem = document.getElementById(domId);
        elem.addEventListener("click", bind(button, "click", callback), false);
        function bind(context, name, callback) {
            return function () {
                return context[name].apply(context, [callback]);
            };
        }
        function active() {
            button.enable = true;
        }
    }

</script>