The previous words
One day, I had a whim and repeatedly clicked the refresh button of the refresh blog post list on the homepage of the blog park several times within 1 minute. Sure enough, IP was disabled at that time. Later, restart your router and re-acquire the IP to access the homepage of the blog park. So, is it better to set a limited time (such as 1 second) to prevent the button from being clicked repeatedly?
Idea 1
The most direct idea may be to unbind the event binding function of the button after clicking the button, and rebind the function after 1s.
<button id="btn">0</button><script>btn.onclick = function add(){ btn.innerHTML = Number(btn.innerHTML) + 1; btn.onclick = null; clearTimeout(timer); var timer = setTimeout(function(){ btn.onclick = add; },1000); }</script>Idea 2
Another idea is to obtain and record the time. When clicking again, the time interval is greater than 1s.
<button id="btn">0</button><script>btn.onclick = (function(){ var last = Date.now(); return function(){ var now = Date.now(); if((now - last)>1000){ btn.innerHTML= Number(btn.innerHTML) + 1; } last = now; }})();</script>The above detailed explanation of the ideas of preventing repeated clicks of buttons within a limited time in JavaScript is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.