Click to See Complete Forum and Search --> : Javascript setTimeout
Thomas2
07-23-2003, 04:56 AM
Does anybody know how to use the Javascript setTimeout object without calling a function ? I tried everything but nothing else works. Most Javascript sources indicate that the first parameter of the function can be any Javascript statement, but I have not seen it used anywhere without a function call. I just want to hold the execution of a for-loop for a specified time at each pass.
At the moment I have programmed around this problem by using a function that recursively calls itself from the setTimeout object and breaks after a given number of passes, but I find this rather unsatisfactory.
AdamBrill
07-23-2003, 07:53 AM
Is this what your looking for?
<script type="text/javascript">
x=0;
test = setInterval("alert('x = '+x); x>10? window.clearInterval(test):x++; ",100);
</script>
It would be easier to just set a setInterval on a function, though....
Thomas2
07-23-2003, 04:16 PM
Thanks for the suggestion, but this is not much more elegant than my present solution.
I want to load some images in a delayed sequence and thought originally this would work
for(i=1; i<=6; i++) {setTimeout("document.images[i].src='image'+i+'.jpg';" , 500); }
but it doesn't. Strangely, what happens is that it tries to display image number 7 (which is outside the loop) but none of the others. It seems that the loop variable gets confused by the setTimeout as it works without it (obviously without delay then).
AdamBrill
07-23-2003, 05:11 PM
You could do that with the method I posted above. Basically, like this:<script type="text/javascript">
x=0;
test = setInterval("document.images[x].src='image'+x+'.jpg'; x>10? window.clearInterval(test):x++; ",500);
</script> I hope that helps. ;)
Thomas2
07-24-2003, 03:33 AM
Do you have any explanation then why
for(i=1; i<=6; i++) {setTimeout("document.images[i].src='image'+i+'.jpg';" , 500); }
does not work ?
Gollum
07-24-2003, 03:53 AM
Yes!
You are using the variable 'i' in two completely different scopes here.
firstly, in the for loop, you are counting 1 to 6, then six times, in the setTimeout handler (long after your original i bit the dust) you are running code like this...
document.images[i].src='image'+i+'.jpg';"
At this point Javascript doesn't know what i is anymore.
you could try this instead...
for(i=1; i<=6; i++) {setTimeout("document.images[" + i + ].src='image" + i + ".jpg';" , 500); }
notice the subtle difference in ' and " being used here.
AdamBrill
07-24-2003, 08:41 AM
Gollum, your way won't work any better. What is happening is that you are running the for loop 6 times right in a row setting timeouts. It sets all of the timeouts in approximately .01 seconds. ;) So, what will happen is it will wait the right time the first time, but then all of the timeouts will run out at almost the same time, thus making it not work correctly. Gollum's method will only take a little longer the first time... Also, by the time the for loop is done running(before any of the setTimeouts run), i is already at 7. If you want to use a for loop, you would need eval and it would look like this:for(i=1; i<=6; i++) {eval("setTimeout('document.images[" + i + "].src=\"image" + i + ".jpg\";' , 500*"+i+")"); }Which is not very pretty to look at. :D I vote for my original method... ;)<script type="text/javascript">
x=0;
test = setInterval("document.images[x].src='image'+x+'.jpg'; x>10? window.clearInterval(test):x++; ",500);
</script>
Thomas2
07-25-2003, 09:53 AM
Originally posted by AdamBrill
If you want to use a for loop, you would need eval and it would look like this:for(i=1; i<=6; i++) {eval("setTimeout('document.images[" + i + "].src=\"image" + i + ".jpg\";' , 500*"+i+")"); }
Actually
for(i=1; i<=6; i++) { setTimeout("document.images["+i+"].src='image"+i+".jpg';", i*500); }
works as well (and doesn't look too bad).
I realize now that part of my initial problem was caused by not taking the loop variable out of the string in the array argument, but I assumed anyway wrongly that the setTimeout function would stop the script completely for the specified time, but apparently the for-loop continues and all the setTimeout handlers are started virtually simultaneously if the delay is not incremented at each pass.
Anyway, thanks again for your help.
AdamBrill
07-25-2003, 10:58 AM
I'm glad I could help. :)