Newbie - Counter script

Discussion in 'Javascript' started by Bundy, Jan 10, 2006.

  1. Bundy

    Bundy Guest

    Hi




    I am trying to write a simple script which counts from 1 to 4.

    Problem - It shows 'Start' waits for 2 second then goes straight to 4.

    I have just strarted programming so please keep it simple.

    Thanks

    Bundy


    My script

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <script language="JavaScript" type="text/JavaScript">
    var abc = 0;

    function every_second()
    {
    for (x=1; x<5; x++)
    {
    setTimeout("second_function()",2000);
    }
    }

    function second_function()
    {
    para1.innerText = abc;
    abc++;
    }

    </SCRIPT>
    </head>
    <body onLoad="every_second();">
    <p id="para1" > Start </p>
    </body>
    </html>
    Bundy, Jan 10, 2006
    #1
    1. Advertising

  2. Bundy

    Evertjan. Guest

    Bundy wrote on 10 jan 2006 in comp.lang.javascript:

    > <script language="JavaScript" type="text/JavaScript">


    language="JavaScript" do not use anymore!

    > var abc = 0;
    >
    > function every_second()
    > {
    > for (x=1; x<5; x++)
    > {
    > setTimeout("second_function()",2000);
    > }
    >}


    All 4 timeouts staart at nearly the same time and fire at nearly the same
    time

    > function second_function()
    > {
    > para1.innerText = abc;


    ..innerText is IE only!

    > abc++;
    > }
    >
    > </SCRIPT>
    > </head>
    > <body onLoad="every_second();">
    > <p id="para1" > Start </p>
    > </body>
    > </html>
    >



    Try:

    <script type="text/JavaScript">
    var abc = 0;
    function myFunction(){
    para1.innerHTML = abc++;
    setTimeout('myFunction()',2000);
    }
    </script>

    <body onLoad='myFunction();'>
    <p id='para1'>x</p>




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jan 10, 2006
    #2
    1. Advertising

  3. Bundy

    Bundy Guest

    [SOLVED] - Newbie - Counter script

    Evertjan. wrote:
    > Bundy wrote on 10 jan 2006 in comp.lang.javascript:
    >
    >
    >><script language="JavaScript" type="text/JavaScript">

    >
    >
    > language="JavaScript" do not use anymore!
    >
    >
    >>var abc = 0;
    >>
    >>function every_second()
    >>{
    >> for (x=1; x<5; x++)
    >> {
    >> setTimeout("second_function()",2000);
    >> }
    >>}

    >
    >
    > All 4 timeouts staart at nearly the same time and fire at nearly the same
    > time
    >
    >
    >>function second_function()
    >>{
    >> para1.innerText = abc;

    >
    >
    > .innerText is IE only!
    >
    >
    >> abc++;
    >> }
    >>
    >></SCRIPT>
    >></head>
    >><body onLoad="every_second();">
    >><p id="para1" > Start </p>
    >></body>
    >></html>
    >>

    >
    >
    > Try:
    >
    > <script type="text/JavaScript">
    > var abc = 0;
    > function myFunction(){
    > para1.innerHTML = abc++;
    > setTimeout('myFunction()',2000);
    > }
    > </script>
    >
    > <body onLoad='myFunction();'>
    > <p id='para1'>x</p>
    >
    >
    >
    >


    Thanks.

    To stop script counting to 4 added inside funtion
    if(abc<5)
    {
    setTimeout('myFunction()',2000);
    }
    else
    {
    para1.innerHTML = "finishing counting to four";
    return;
    }

    Bundy
    Bundy, Jan 10, 2006
    #3
  4. Bundy

    Lee Guest

    Bundy said:

    >function every_second()
    >{
    > for (x=1; x<5; x++)
    > {
    > setTimeout("second_function()",2000);
    > }
    >}


    When posting code, please convert TAB characters to spaces.

    The setTimeout() function does not insert any delay.
    It simply schedules the expression to be evaluated after the
    specified delay. Once it has been scheduled (which takes
    almost no time at all), processing continues. In your case,
    it then immediately schedules the next invocation of your
    second_function().

    The method that Evertjan has posted is generally preferred,
    but as an illustration, here's another way to do what you
    seem to want. Each invocation is scheduled to occur two
    seconds later than the previous one:

    function every_second()
    {
    for (x=1; x<5; x++)
    {
    setTimeout("second_function()",2000*x);
    }
    }
    Lee, Jan 10, 2006
    #4
  5. Bundy

    Evertjan. Guest

    Re: [SOLVED] - Newbie - Counter script

    Bundy wrote on 10 jan 2006 in comp.lang.javascript:

    >> Try:
    >>
    >> <script type="text/JavaScript">
    >> var abc = 0;
    >> function myFunction(){
    >> para1.innerHTML = abc++;
    >> setTimeout('myFunction()',2000);
    >> }
    >> </script>
    >>
    >> <body onLoad='myFunction();'>
    >> <p id='para1'>x</p>
    >>
    >>
    >>
    >>

    >
    > Thanks.
    >
    > To stop script counting to 4 added inside funtion
    > if(abc<5)
    > {
    > setTimeout('myFunction()',2000);
    > }
    > else
    > {
    > para1.innerHTML = "finishing counting to four";
    > return;
    > }
    >


    The return has no function at the end of a function.

    try:

    <script type="text/JavaScript">
    var abc = 0;
    function myFunction(){
    if (abc=4){
    para1.innerHTML = "finished counting at four";
    return;
    }
    para1.innerHTML = abc++;
    setTimeout('myFunction()',2000);
    }
    </script>

    <body onLoad='myFunction();'>
    <p id='para1'>x</p>



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jan 10, 2006
    #5
  6. Bundy

    Bundy Guest

    Re: [SOLVED] - Newbie - Counter script

    Evertjan. wrote:
    > Bundy wrote on 10 jan 2006 in comp.lang.javascript:
    >
    >
    >>>Try:
    >>>
    >>><script type="text/JavaScript">
    >>> var abc = 0;
    >>> function myFunction(){
    >>> para1.innerHTML = abc++;
    >>> setTimeout('myFunction()',2000);
    >>> }
    >>></script>
    >>>
    >>><body onLoad='myFunction();'>
    >>><p id='para1'>x</p>
    >>>
    >>>
    >>>
    >>>

    >>Thanks.
    >>
    >>To stop script counting to 4 added inside funtion
    >> if(abc<5)
    >> {
    >> setTimeout('myFunction()',2000);
    >> }
    >> else
    >> {
    >> para1.innerHTML = "finishing counting to four";
    >> return;
    >> }
    >>

    >
    >
    > The return has no function at the end of a function.
    >
    > try:
    >
    > <script type="text/JavaScript">
    > var abc = 0;
    > function myFunction(){
    > if (abc=4){
    > para1.innerHTML = "finished counting at four";
    > return;
    > }
    > para1.innerHTML = abc++;
    > setTimeout('myFunction()',2000);
    > }
    > </script>
    >
    > <body onLoad='myFunction();'>
    > <p id='para1'>x</p>
    >
    >
    >

    One small error, in 'if(abc=0)' replace = with ==.

    I prefer your code.

    Thanks

    Bundy
    Bundy, Jan 11, 2006
    #6
  7. Bundy

    Bundy Guest

    [SOLVED AGAIN] Newbie - Counter script

    Lee wrote:
    > Bundy said:
    >
    >
    >>function every_second()
    >>{
    >> for (x=1; x<5; x++)
    >> {
    >> setTimeout("second_function()",2000);
    >> }
    >>}

    >
    >
    > When posting code, please convert TAB characters to spaces.
    >
    > The setTimeout() function does not insert any delay.
    > It simply schedules the expression to be evaluated after the
    > specified delay. Once it has been scheduled (which takes
    > almost no time at all), processing continues. In your case,
    > it then immediately schedules the next invocation of your
    > second_function().
    >
    > The method that Evertjan has posted is generally preferred,
    > but as an illustration, here's another way to do what you
    > seem to want. Each invocation is scheduled to occur two
    > seconds later than the previous one:
    >
    > function every_second()
    > {
    > for (x=1; x<5; x++)
    > {
    > setTimeout("second_function()",2000*x);
    > }
    > }
    >


    Thanks

    This had me perplexed, now I understand.

    Bundy
    Bundy, Jan 11, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Eeediot
    Replies:
    3
    Views:
    2,224
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  2. Mark Hobley

    Running a CGI Counter Script

    Mark Hobley, Dec 7, 2005, in forum: HTML
    Replies:
    10
    Views:
    849
    Neredbojias
    Dec 9, 2005
  3. George2
    Replies:
    1
    Views:
    791
    Alf P. Steinbach
    Jan 31, 2008
  4. T Stewart

    Problem with counter CGI script

    T Stewart, Jul 5, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    211
    Gunnar Hjalmarsson
    Jul 7, 2003
  5. Randy Webb

    word counter script locks up Mozilla

    Randy Webb, Feb 15, 2004, in forum: Javascript
    Replies:
    6
    Views:
    122
    David
    Feb 16, 2004
Loading...

Share This Page