JQuery button only presses every 2 seconds

I am having problems with some jQuery code when I click the button very quickly. To solve this problem, I only need to allow users to press a button every 2 seconds or so. How can I make a button pressed only every 2 seconds (or whatever I choose)?

+4
source share
3 answers

Assuming you are using <button> or <input> ,

 $('#yourButton').click(function(){ var btn = $(this); btn.prop('disabled',true); window.setTimeout(function(){ btn.prop('disabled',false); },2000); }); 

jsFiddle DEMO

2000 is the time that the button turns on again, in milliseconds

+12
source

Ask the "click" handler to add the class ("clicking", possibly) to the button, and also start the timer to delete the class after 2 seconds. If the handler sees that the “click” is already applied to the button, it does nothing.

You can also disable the button; this might be better as it will give the user a visual indication that the button will do nothing.

+1
source

Disable the button when clicked and start the timer with a 2 second timeout, which will activate the button again when it expires.

+1
source

All Articles