Click to See Complete Forum and Search --> : Double Clicking Error - This is why i hate Javascript


Superfly1611
09-15-2003, 09:59 AM
OK - got another Javascript error for ya.
I've written an onclick even for a html button which updates the value of a text box.


function minus(Pid)
{
Product = document.getElementById(Pid);
if (Product.value > 1)
{
Product.value--;
}
}



This works fine.

But there's a small bug.... If you click the button twice in relatively quick succession (but not a proper double click) it doesn't seem to register the second click.

Is this just a generic Javascript bug or is this something that can be avoided?

I would quite happilly do this in ASP as i'm really not a fan of Javascript but i'm not allowed (got to keep network traffic down as much as possible).

Any ideas?

Khalid Ali
09-15-2003, 10:03 AM
setting a double click is not very prudent thing to do,if you have an elderly or younger client they might not use double click as you might expect,now that does not mean that JavaScript has this problem,that means the user is not doing a proper action,and on your part bad design, try sticking with single click

Superfly1611
09-15-2003, 10:21 AM
No no no......

I am sticking with single click..... but if you do two single clicks say half a second apart it doesn't run the function twice

Khalid Ali
09-15-2003, 10:39 AM
add return true at the end of function,am sure this will make it work(logically it will work once first execution of the function is complete)

if nothing seem to work,then post the link where you are trying to implement.

Superfly1611
09-16-2003, 03:55 AM
OK - here is full code/html listing of the function.


<html>
<head>

<script language="javascript" SRC="JScript/CommonFunctions.js"></script>
<script language=javascript>
<!--
function minus(Pid)
{
Product = document.getElementById(Pid);
if (Product.value > 1)
{
Product.value--;
return true;
}
}
//-->
</script>
</head>
<body>
<input type="button" onclick="minus('0001');" value="minus">
<input type="text" id="0001" name="0001" value=10>
</body>
</html>



I'm getting the impression i'm confusing (grammar never was my strong point) you so i thought i'd recap what's going on....

Using the above code i AM able to reduce the value in the text box.
But If the user clicks the button twice in relatively quick succession (but not quick enough to be considered a double click) it DOES NOT run the script twice it only runs it once.

For all i know this is something unavoidable in Javascript but i thought i'd check with you guys.

Webskater
09-16-2003, 05:23 AM
I am always concerned to make sure users do not enter records twice by accidentally clicking a button twice. So, as soon as the button is clicked I disable it and then enable it again when the function has finished. I would guess that for the few milliseconds that it takes the function to execute, nothing on the page has focus - which is why your button will not work while the function is executing.

Superfly1611
09-16-2003, 05:36 AM
cheers Webskater....

This work to a limited degree of success.
It now stops the user from pressing the button until it is ready to accept teh function again. So now on the second click the button doesn't even get depressed. Which is somethig i can work with. (But not ideal)

I'll be amazed if there aren't people in this community that haven't had similar problems.

Thanks again Webskater