Link to home
Start Free TrialLog in
Avatar of wilfordrocks
wilfordrocks

asked on

Disable a button for 2 seconds

I just want a suggestion from someone with experience.  I would like to disable a button ( and possibly make it grey) for a second or two after it was clicked.  After 2 seconds it should re-enable so if the user really wants to click it multiple times they can.
Thank you in advance or your advice.
Avatar of ktaczala
ktaczala
Flag of United States of America image

Set up a separate thread(using BackgroundWorker) to count for 2 seconds.  Then when the thread completes, re-enable the button.  By putting it on a separate thread normal operation will not pause in your application.
Put a Timer Control on the Form and set its Interval property to 2000.

In the Click event of the button, start the timer and disable the button:

            Timer1.Start()
            Button1.Enabled = False

In the Tick event of the timer, reenable the Button

            Button1.Enabled = True
Avatar of wilfordrocks
wilfordrocks

ASKER

That is kind of a windows way of doing it.  I suspect Java or jquery has a browser way of doing it.  thanks for your input.  I will leave the question open a little longer.
Ooops. Sorry, I did not see your Tags.

Try to be more precise in your Topics. ExpertExchange uses the topics to flag experts, and you have more chances of having your question falling in the right hands if you specify less general topics.
With plain javascript you could use something like this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>EE Q_28335925</title>

<script type='text/javascript'>
var g_sbm = null;
function temp_disable(sbm, tm) {
    g_sbm = sbm;
    toggle_sbm();
    setTimeout(toggle_sbm, tm);
}
function toggle_sbm() {
    g_sbm.disabled = !g_sbm.disabled;
}
</script>

</head>
<body>

<form name="frm" action="" method="post" onsubmit="return false;">
  <input name="name">
  <input name="sbm" id="sbm" type="submit" onclick="temp_disable(this, 2000)">
</form>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is beautiful and elegant.  Thank you.
Just to make sure I understand the variable names.
'g_'  means global?
'sbm'  stands for submit?
'col'  is color?

Again, thank you.
yep, 3 out of 3 :-)

And thank you!