Disabling a double click on a submit button
Greetings,
I wrote a simple jquery snippet to prevent a double click on a submit button and it works.
Button id = 'swl_submit'.
<script type="text/javascript">
$(document).ready(function() {
$('#swl_submit').click(function(){
$(this).attr('disabled', 'disabled');
setTimeout(restoreButton,1000);
});
});
function restoreButton(){
$('#swl_submit').attr('disabled','');
}
</script>
This works fine. Then, I thought I could shorten this up a bit by adding the code in my restoreButton function to the setTimeoutline, like so:
<script type="text/javascript">
$(document).ready(function() {
$('#swl_submit').click(function(){
$(this).attr('disabled', 'disabled');
setTimeout($('#swl_submit').attr('disabled',''),1000);
});
});
</script>
This doesn't work. The button does get disabled, but only for an instant. Not enough to prevent a double-click. It seems it enables the button before waiting the 1 second. I've replaced the "$('#swl_submit').attr('disabled','')" with 'alert("working")' and the button is disabled and there is a one second delay before the alert pops up.
I'd like to use this shorthand way because I'd like to not have to code a restoreButton function on each page.
any thoughts?
thanks,
JC