Results 1 to 10 of 10

Thread: setTimeOut !!!

  1. #1
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default setTimeOut !!!

    hi there,

    I got this code from W3Schools and this is the link to the original code http://www.w3schools.com/js/tryit.as...iming_infinite and I played around with it, but I dont understand why isn't it working!!! I tried all ways!!!

    <html>
    <head>
    <script type="text/javascript">
    var d
    var t
    function a(name){
    var c = name.txt1.value
    timedCount(c)
    }
    function timedCount(d)
    {
    var x = d
    document.getElementById('txt').value=x
    x=x+1 // or x++
    var l = x.value
    document.getElementById('txt').value=x
    t=setTimeout("timedCount(l)",1000)
    }
    </script>
    </head>

    <body>
    <form name="next">
    <input type="button" value="Start count!" onClick="a(this.form)">
    <input type="text" id="txt">
    <input type="text" id="txt1">
    </form>
    <p>Click on the button above. The input field will count for ever, starting at 0.</p>
    </body>

    </html>

    thank you in advance.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The string you passed to setTimeout() is evaluated in the global scope, where l isn't defined. Use a closure:
    Code:
    setTimeout(function() { timedCount(l); }, 1000);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you for your answer but I tried that code replasing setTimeOut in my code to the one you gave me but didnt work!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Nothing wrong with Twey's advice here. The rest of your code just isn't working. What's this for anyway?

    Try this:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var d
    var t
    function a(name){
    var c = name.txt1.value
    timedCount(c)
    }
    function timedCount(d)
    {
    var x = d
    document.getElementById('txt').value=x
    x=x+1 // or x++
    var l = x - 0;
    document.getElementById('txt').value=x
    t=setTimeout(function(){timedCount(l);},1000);
    }
    </script>
    </head>
    
    <body>
    <form name="next">
    <input type="button" value="Start count!" onClick="a(this.form)">
    <input type="text" id="txt">
    <input type="text" id="txt1">
    </form>
    <p>Click on the button above. The input field will count for ever, starting at 0.</p>
    </body>
    
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    And, this is how I would have written it:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Counter - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form action="javascript:void(0);" onsubmit="return false;">
    <div><input type="button" value="Start Count!"
    onclick="this.disabled = 1; var i = this.form.elements.txt, t =
    function(x){i.value=x++;setTimeout(function(){t(x);},1000);};t(0);">
    <input name="txt" readonly type="text"></div></form>
    <p>Click the above button. It will count 'forever', starting at 0.</p>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a lot guys. John your code is almost perfect. Sorry that i haven't explained it yet, but what i was trying to reach is to get the variable from input2 and post it into input1 and start counting from there.
    Ex: if i wrote 78 in input 2, once i click start, it should take that number and post it in input1 and count 78, 79, 80 ... etc.

    It's stupid but I am learning.

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Counter - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form action="javascript:void(0);" onsubmit="return false;">
    <div><input type="button" value="Start Count!" onclick="var o = this.onclick,
    e=this.form.elements,v='value',x; if(isNaN(x=e.txt1[v]-0)){x=0};e.txt[v]=x++;
    if(o.t){clearInterval(o.t)};o.t=setInterval(function(){e.txt[v]=x++;}, 1000);">
    <input name="txt" readonly type="text">
    <div>Click the above button. It will count 'forever', starting at:</div>
    <input name="txt1" type="text" value="0"></div></form>
    </body>
    </html>
    Last edited by jscheuer1; 11-10-2007 at 04:47 PM. Reason: streamline onclick
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    OK, this code is now long past the length where it requires formatting
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Counter - Demo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      </head>
      <body>
        <form action="javascript:void(0);" onsubmit="return false;">
          <div>
            <input
                type="button"
                value="Start Count!"
                onclick="
                  var o = this.onclick,
                    e = this.form.elements,
                    v = 'value',
                    x;
    
                  if(isNaN(x = +e.txt1[v]))
                    x = 0;
    
                  e.txt[v] = x++;
    
                  if(o.t)
                    clearInterval(o.t);
    
                  o.t = setInterval(function() {
                    e.txt[v] = x++;
                  }, 1000);">
            <input name="txt" type="text" readonly>
            <p>Click the above button. It will count 'forever', starting at:</p>
            <input name="txt1" type="text" value="0">
          </div>
        </form>
      </body>
    </html>
    Don't forget that just because we can parse a big chunk of unformatted code, doesn't mean that the original poster can -- and this one even made me look twice

    I thought you were a detractor of unnecessary braces?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I like braces in close quarters. Sometimes they are required there. Rather than guess, I just throw them in.

    I don't think that's quite how I would have written out a long version, but thanks Twey. It should be a little easier to follow like that. For one, if I were bothering to 'go longhand', I would have moved the script code to a function in the head, changing a references or two in it, passing the input to it as 'this', its sole argument.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I was a little bored, so I made my own annotated version of my code, with some extra markup and styles thrown in as 'eye candy', or whatever (only the script code is annotated):

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Counter - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    fieldset {
    width:21.5em;
    }
    #inner {
    padding-top:1ex;
    }
    form {
    margin:0;
    padding:0;
    }
    </style>
    <script type="text/javascript">
    function counter(el){
    var o=el.onclick, //create a reference to the element's onclick function for later shorthand use.
    e=el.form.elements, //condense the verbose and reused multi-object lookup for the form's elemnts for later shorthand use.
    v='value', //create a reference to the value attribute for later shorthand use.
    x; //declare x for later use.
    if(isNaN(x= +e.txt1[v])) //with below line tests if the value of the txt1 input can be number.
    x=0; //if so, it assigns it to x, otherwise sets x to 0.
    e.txt[v]=x++; //sets the txt input's value to x, while at the same time incrementing it for its next use.
    if(o.t) //with below line - if we are already running,
    clearInterval(o.t); //stop to clear the way for a new run.
    o.t=setInterval(function(){e.txt[v]=x++;}, 1000); //set a reference to and initiate the repeating loop of
    //updating by 1 both x and the txt input's value.
    };
    </script>
    </head>
    <body>
    <form action="javascript:void(0);" onsubmit="return false;">
    <fieldset>
    <legend>Counter</legend>
    <div id="inner">
    <input type="button" value="Start Count!" onclick="counter(this)">
    <input name="txt" readonly type="text">
    <div>Click the above button. It will count 'forever', starting at:</div>
    <input name="txt1" type="text" value="0">
    </div>
    </fieldset>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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