SetTimeout not working

Discussion in 'Javascript' started by Darren.Ratcliffe@gmail.com, Aug 14, 2006.

  1. Guest

    Hi

    The below pasted code is my attempt to get the text of a span to change
    every second.

    However, it seems to just set the text of the span to be the last item
    in the array.

    Can anybody help?

    Many thanks in advance

    Daz

    <script language="javascript" type="text/jscript">

    function DoWait()
    {

    vAirlines = new Array("Wind", "Rain", "Fire", "Earth");

    for (i=0;i<vAirlines.length-1;i++)
    {
    string = "document.getElementById(\"spAirlineCount\").innerHTML = \""
    + vAirlines + "\"";

    setTimeout(string, 1000);
    }
    }

    DoWait();


    </script>

    <span id="spAirlineCount">000</span>
    , Aug 14, 2006
    #1
    1. Advertising

  2. Evertjan. Guest

    wrote on 14 aug 2006 in comp.lang.javascript:

    > setTimeout(string, 1000);
    >


    a string is not a command in the sense of setTimeout



    > for (i=0;i<vAirlines.length-1;i++)
    > {
    > string = "document.getElementById(\"spAirlineCount\").innerHTML = \""
    >+ vAirlines + "\"";
    >
    > setTimeout(string, 1000);
    > }
    >}


    Anyway, your 4(?) strings would execute at once,
    being all 1 second after the for loop.
    so what is the sense?

    A setTimout() is NOT a wait() function found in other languages.

    Try:

    ========================
    <div id='spAirlineCount'>-----</div>

    <script type='text/javascript'>

    var vAirlines = new Array("Wind", "Rain", "Fire", "Earth");
    var theDiv = document.getElementById('spAirlineCount');
    var i=0;
    setTimeout('showOne()',1000);

    function showOne(){
    theDiv.innerHTML = vAirlines[i++];
    if (i < vAirlines.length)
    setTimeout('showOne()',1000);
    };

    </script>
    =========================


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

  3. Guest

    Hi Evertjan

    I was just coming to that conclusion after looking at this
    http://www.jsmadeeasy.com/javascripts//Text
    Animations/text_rotation_userBrowDetection/text_rotation_userBrowDetection.htm

    Many thanks for your help.

    Darren
    Evertjan. wrote:
    > wrote on 14 aug 2006 in comp.lang.javascript:
    >
    > > setTimeout(string, 1000);
    > >

    >
    > a string is not a command in the sense of setTimeout
    >
    >
    >
    > > for (i=0;i<vAirlines.length-1;i++)
    > > {
    > > string = "document.getElementById(\"spAirlineCount\").innerHTML = \""
    > >+ vAirlines + "\"";
    > >
    > > setTimeout(string, 1000);
    > > }
    > >}

    >
    > Anyway, your 4(?) strings would execute at once,
    > being all 1 second after the for loop.
    > so what is the sense?
    >
    > A setTimout() is NOT a wait() function found in other languages.
    >
    > Try:
    >
    > ========================
    > <div id='spAirlineCount'>-----</div>
    >
    > <script type='text/javascript'>
    >
    > var vAirlines = new Array("Wind", "Rain", "Fire", "Earth");
    > var theDiv = document.getElementById('spAirlineCount');
    > var i=0;
    > setTimeout('showOne()',1000);
    >
    > function showOne(){
    > theDiv.innerHTML = vAirlines[i++];
    > if (i < vAirlines.length)
    > setTimeout('showOne()',1000);
    > };
    >
    > </script>
    > =========================
    >
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)
    , Aug 14, 2006
    #3
  4. JRS: In article <>
    , dated Mon, 14 Aug 2006 07:43:47 remote, seen in
    news:comp.lang.javascript, posted :

    >The below pasted code is my attempt to get the text of a span to change
    >every second.


    > ...


    > setTimeout(string, 1000);


    Other considerations apart, if you want it to change exactly once every
    second in every browser you will need to change that second argument, to
    allow for timing overheads and rounding.

    See <URL:http://www.merlyn.demon.co.uk/js-date0.htm#TaI>.

    PS - Are there combinations of browser and OS for which Tock() does not
    always show N.1 seconds plus a varying bit, or for which Tick() does
    show that?

    function DP3(T) { return String(T).replace(/(\d\d\d)$/, '.$1') }

    function Tick() { var D = new Date()
    setTimeout("Tick()", 1000) // Drifty
    DynWrite("ic", DP3(D.getTime())) }

    function Tock() { var D = new Date()
    setTimeout("Tock()", 1100-D%1000) // Synced
    DynWrite("oc", DP3(D.getTime())) }

    OP : read the newsgroup FAQ.
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Aug 14, 2006
    #4
    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. =?Utf-8?B?RQ==?=

    javascript setTimeout does not work

    =?Utf-8?B?RQ==?=, May 2, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    8,756
    =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
    May 2, 2006
  2. tshad
    Replies:
    4
    Views:
    157
    tshad
    Dec 3, 2004
  3. Athanasius

    setTimeout not working on Mac IE 5.2

    Athanasius, May 3, 2005, in forum: Javascript
    Replies:
    2
    Views:
    414
    Richard Cornford
    May 7, 2005
  4. James Black

    setTimeout not working on IE

    James Black, Apr 14, 2006, in forum: Javascript
    Replies:
    2
    Views:
    642
    Randy Webb
    Apr 14, 2006
  5. Shan

    SetTimeout not working with IE

    Shan, Feb 22, 2008, in forum: Javascript
    Replies:
    4
    Views:
    118
    Thomas 'PointedEars' Lahn
    Feb 24, 2008
Loading...

Share This Page