js 防止重復點擊


1、添加flag 

     適用於ajax 表單提交,提交之前flag = false , 提及中,true ,提交后false

2、事件重復點擊:

    

<script>
var throttle = function (fn, delay) {
    var timer = null;
    return function () {
        var args = arguments; //參數集合
        console.log("arguments",arguments);
        console.log("timer",timer);
        clearTimeout(timer);//重復執行的都clear掉了
        timer = setTimeout(function () {
            fn.apply(this, args);
        }, delay);
    }
}


/**
 * 要執行的方法
 * @param String name 傳遞的參數
 */
function postFun(name) {
    document.writeln("name:" + name);
}

//================測試部分 => 【1s重復點擊10次】
var t = throttle(postFun, 100);
var ejector = setInterval(() => {
    t("333");
}, 100); // 10 ci 

setTimeout(() => {
    clearInterval(ejector);
}, 1000); 
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM