javascript - Adding a timed delay after pressing a button to prevent button spam -
say have 4 buttons
<button id="one">one</button> <button id="two">two</button> <button id="three">three</button> <button id="four">four</button>
and prevent spam, want make whenever of buttons pressed, none of them able pressed again next 0.6 seconds.
how might achieve this?
you can use javascript code:
var btns = document.getelementsbytagname('button'); for(var i=0;i<btns.length;i++){ btns[i].addeventlistener('click', function(){ disablebuttons(true); settimeout(function(){disablebuttons(false);}, 600); }); } function disablebuttons(state){ for(var i=0;i<btns.length;i++){ btns[i].disabled = !!state; } }
of course, need run code after page loaded.
Comments
Post a Comment