Why it does not give me timeout?

Q

QA

Array.prototype.shuffle= function(times){
var i,j,t,l=this.length;
while(times--){
with(Math){
i=floor(random()*l);j=floor(random()*l);}
t=this;this=this[j];this[j]=t;
}
return this;
}
var st=[
"http://javascript.internet.com/img/image-cycler/01.jpg",
"http://javascript.internet.com/img/image-cycler/02.jpg",
"http://javascript.internet.com/img/image-cycler/03.jpg",
"http://javascript.internet.com/img/image-cycler/04.jpg"
]


st.shuffle(200);

function donothing () {return true;}

for(j=0;j<st.length;j++){

setTimeout('donothing()', 2000);
//rImage.src = st[j];
//rText.value = st[j];
document.write (st[j]);
}

**************************************
I want to display them every 2 second. After everything is displayed it then
stops.
But it does not give me the 2s delay. It displays everything once.
 
E

Evertjan.

QA wrote on 10 sep 2004 in comp.lang.javascript:
function donothing () {return true;}

for(j=0;j<st.length;j++){

setTimeout('donothing()', 2000);
document.write (st[j]);
}

**************************************
I want to display them every 2 second. After everything is displayed
it then stops.
But it does not give me the 2s delay. It displays everything once.

Because you are doing nothing once.

If you would de setInterval you would be doing nothing [as advised
elswhere] every two seconds, which is an improvement but does not help,
as you want something done, as I presume.

SetTimeout() and set()interval start a seperate process after the stated
amout of miliseconds, they do not "wait" the script for that in their
place.

Furthermore, using document.write() after setup time will destroy the
page INCLUDING the javascript itself.

============================================

Try this code as a seperate HTML file

<div id='myDiv'>Start:<br></div>

<script type='text/javascript'>
var j=0;
myFunction();

function myFunction(){
d = document.getElementById('myDiv');
d.innerHTML+= j++ + "<br>";
if (j<30) setTimeout('myFunction()',500);
};
</script>
 
D

Dr John Stockton

JRS: In article <7%[email protected]
om>, dated Fri, 10 Sep 2004 06:40:03, seen in
QA said:
Array.prototype.shuffle= function(times){
var i,j,t,l=this.length;
while(times--){
with(Math){
i=floor(random()*l);j=floor(random()*l);}
t=this;this=this[j];this[j]=t;
}
return this;
}


That's not a good way to shuffle; you should read the newsgroup FAQ, and
<URL: http://www.merlyn.demon.co.uk/js-randm.htm>.

Also, l is a poor choice of identifier, at least in News; it cal look
too much like 1.
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top