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 14 of 14
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    newbie. Creating up/down counters the user sees

    Would someone kindly point me to an example of an on-screen counter which runs up (or down) as the user holds down a button, stops when the user lifts off?

    No doubt this is dead simple and a to ask, but my book does not carry an example.
    Last edited by Roy Gardiner; 07-15-2004 at 01:50 PM.

  • #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
    Something like this?:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    script language="JavaScript" type="text/JavaScript">
    n=0;
    timer=1000;//delay in milliseconds
    function counter(q){
    if(
    q=='+'){n++}
    else{
    n--}
    document.getElementById('count').innerHTML=n
    con 
    setTimeout('counter(\''+q+'\')',timer)
    }
    function 
    stopC(){
    clearTimeout(con)
    }
    </script>
    </head>
    <body>
    <input type="button" value="+" onmousedown="counter('+')" onmouseup="stopC()">
    &nbsp; 
    <input type="button" value="-" onmousedown="counter('-')" onmouseup="stopC()">
    <br>
    <br>
    <div id="count">0</div>
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Exactly, thank you Mr KOR.

    Aren't these forums wonderful things?

    Now all I have to do is figure out how it works...

  • #4
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    On looking at the code, Mr KOR, it looks like an example created to illustrate the solution to my problem, rather than one taken from existing code.

    If so that's

    as well as the in my first thank-you.

  • #5
    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

    Thumbs up

    it looks like an example created to illustrate the solution to my problem
    Exactly. I sow your post, I knew how to do it, not very intricate when you know how, and post the replay. We all are glad when we can help anyone, as we were at the time, helped gracefully by others
    Cheers
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nice...

    I like that. Throw in an if statement to stop the counter if n == 0.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually there's a very nice example of the desired script in the "Post a Javascript" forum: http://www.codingforums.com/showthread.php?t=34958

    (Make sure you go to the bottom of the thread to get the latest version).

    The advantage of the one posted there is that it can be used several times on the same page without having to duplicate the code. If you decide to use it make sure you also give a thank you to GlennV who wrote it.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #8
    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
    To Number_Ninteen

    PHP Code:
    <script language="JavaScript" type="text/JavaScript"
    n=0
    timer=1000;//delay in milliseconds 
    function counter(q){ 
    if(
    q=='+'){n++} 
    else{
    n--} 
    document.getElementById('count').innerHTML=n;
    if(
    n==0){stopC()}
    else{ 
    con setTimeout('counter(\''+q+'\')',timer
    }
    }
    function 
    stopC(){ 
    clearTimeout(con

    </script> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Roy Sinclair
    Actually there's a very nice example of the desired script in the "Post a Javascript" forum: http://www.codingforums.com/showthread.php?t=34958

    (Make sure you go to the bottom of the thread to get the latest version).

    The advantage of the one posted there is that it can be used several times on the same page without having to duplicate the code. If you decide to use it make sure you also give a thank you to GlennV who wrote it.
    hmm, well something's not right here. I went to the end and saved the last version I saw, but for me now none of it works except the military hours counter. And the script is huge by comparison. I'm on IE6.0.

    For the newbie like me (a minority, I know) Mr KOR's version - small, neat and to the point - is much more educational.

    I don't intend criticism of GlennV (far from it) just to say that the sophisticates here will understand his solution and for any simpletons like me a smaller version works better (thanks again).

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Roy Gardiner
    hmm, well something's not right here. I went to the end and saved the last version I saw, but for me now none of it works except the military hours counter. And the script is huge by comparison. I'm on IE6.0.
    But did you take note of the error I found?
    Quote Originally Posted by glenngv
    Good catch but there should be no .value because you're passing a number not a textfield object.

    (me.hasLeadingZero && defaultValue.value < 10)
    I know the code is long but it is flexible and has advantages which Roy earlier mentioned. I'm not forcing you to use it though.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    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
    Glenn's script is OK as he is a remarcable coder, better than me. It is not a long code regarding to the complex tasks it solves. I was only to give Roy a precise answer to his question, without guessing about a more intricate code (anyway, there was no sense to rebuild it if there is already Glenn's variant which, I repeat, is very good.) Thus, this small quarell has no sense
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #12
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    But did you take note of the error I found?
    Ok now.
    I know the code is long but it is flexible and has advantages which Roy earlier mentioned. I'm not forcing you to use it though.
    Please don't misunderstand, the only reason I commented is that Mr KOR's is a simple solution, yours is (I guess) comprehensive. That I don't understand most of it is my challenge, and is hardly your fault. I am studying and learning now.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    There was no misunderstanding whatsoever. We're here to lay down our suggestions. It's up to you which one to choose. No problem there.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't mean to start anything either, Kor's code is good but I thought it might be worth mentioning Glennv's code too in case you were going to need multiple spinners. The code is also worth examining on it's own just to learn new techniques that might be useful in the future as you write your own code.
    Check out the Forum Search. It's the short path to getting great results from this forum.


  •  

    Posting Permissions

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