Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onClick firing interval?

    Hi:

    Is there anything wrong with this simple function construction:



    var x=0;
    function addX(){
    myForm.Num.value=x;
    x++;
    }

    The reason I ask is, because when using an onClick handler, it doesn't seem to fire as fast as you'd expect, so that you have to pause about a full second in order to get the expected results.


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Is there anything wrong with this simple function construction:
    Nope. It is OK.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by jamescover
    Hi:

    Is there anything wrong with this simple function construction:



    var x=0;
    function addX(){
    myForm.Num.value=x;
    x++;
    }

    The reason I ask is, because when using an onClick handler, it doesn't seem to fire as fast as you'd expect, so that you have to pause about a full second in order to get the expected results.


    -james
    How are you calling the function? Are you using setTimeout or setInterval method to call it?

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How are you calling the function? Are you using setTimeout or setInterval method to call it?


    Hi Glenn:

    No, just calling the function from a button...If I press the button in succession, say, 10 times, it will count to 0-4 or so...I'm talking about intervals as slow as about 2 clicks per sec, the counter only increments by 1. But if I slow it down to 1 click per second, it increments on every click.


    <script>
    <!--

    var x=0;
    function addX(){
    mForm.Num.value=x;
    x++;
    }

    //-->
    </script>

    <form name="mForm">
    <input type="text" name="Num" size="5" />
    <input type="button" value="x++" onClick="javascript:addX();" />
    </form>

    Thanks for responding too Kor.



    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #5
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Hello,

    Try adding an ondblclick="addX()" to the button. Should solve the problem.

    Basscyst
    Last edited by Basscyst; 07-12-2004 at 06:03 PM.
    Helping to build a bigger box. - Adam Matthews

  • #6
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Gecko browsers respond to clicks much differently than IE does: the order of event execution for a double click in Gecko is
    Code:
    mousedown
    mouseup
    click
    mousedown
    mouseup
    click
    dblclick
    In IE, it's missing the second click, so its execution order is
    Code:
    onmousedown
    onmouseup
    onclick
    onmousedown
    onmouseup
    ondblclick
    Wrap the ondblclick event assignment in an if(document.all) branch and you should be set.

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Basscyst--Try adding an ondblclick="addX()" to the button. Should solve the problem.

    Choopernickel-- Wrap the ondblclick event assignment in an if(document.all) branch and you should be set.

    Thanks, for responding.

    Double click works, but are you saying that I can't achieve the same results in IE 5.0 using onClick? I want to increment the value of x on every [single] click...


    thanks,

    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Try using onmousedown instead of onclick or ondblclick

    <input type="button" value="x++" onmousedown="addX();" />

  • #9
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try using onmousedown instead of onclick or ondblclick
    <input type="button" value="x++" onmousedown="addX();" />


    Sorry, Glenn, same result...

    I finally got it sorted, but I think it's something to keep in the back of your mind, if your using js for calculations in a shopping cart, etc.



    <script>
    <!--

    x=0;
    function addX(){
    mForm.Num.value=x;
    x++;
    }

    //-->
    </script>

    <form name="mForm">
    <input type="text" name="Num" size="5" />
    <input type="button" value="x++" onmousedown="javascript:this.focus();" onmouseup="javascript:addX();" />
    </form>


    That's the only way it work increment on every [single] click in IE 5.0...

    Thanks to everyone who contributed.


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    edit: i misread in haste... nevermind my original comment. i saw "onmousedown" and "onmouseup" instead of "onfocus" and "onmouseup"
    Last edited by ]|V|[agnus; 07-13-2004 at 04:32 AM.

  • #11
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so you're going the completely counter-intuitive route just to satiatie IE5? why? doesn't that method result in +2 per click for most others?


    I'm not suggesting that this x-browser cure-all, but that it is something to keep in the back of your mind (i.e, this strange behavior in IE 5.0) when building such an app....

    Although I do have a personal browser preference, I always try to code for NN/IE 4+ browsers, and not impose my own preferences on my viewers.



    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #12
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    see above

  • #13
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi |V|[agnus

    Not a problem.

    I wasn't trying to say anything beyond, I code/design for someone besides myself. For this reason, I intentionally don't use the lastest or the best browser(s), i.e., I don't do this out of personal preference. And I never assume that because something works in my browsers, it will work in all browsers.

    I didn't think that this would result in increments of 2 in other browsers, but I wasn't taking that for granted...


    Cheers


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •