SetTimeout not working

D

Darren.Ratcliffe

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>
 
E

Evertjan.

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>
=========================
 
D

Darren.Ratcliffe

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. said:
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>
=========================
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Mon, 14 Aug 2006 07:43:47 remote, seen in
news:comp.lang.javascript, (e-mail address removed) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top