Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setTimeout not working in for loop 1

Status
Not open for further replies.

AaronBeau

MIS
Jan 29, 2008
89
US
I am using the setTimeout function within a for loop, and for some reason it is not working. I am trying to have the script open a website, wait 5 seconds then close the website. After it closes, it should then open the next website. However, it just opens and closes them instantly, with the exception of the last website, which it closes in 5 seconds

Here is the code:
Code:
<script type="text/javascript">
function openSites() {

var openOneAtATime=new Array()
openOneAtATime[0]="[URL unfurl="true"]http://www.ebay.com"[/URL]
openOneAtATime[1]="[URL unfurl="true"]http://www.google.com"[/URL]
openOneAtATime[2]="[URL unfurl="true"]http://www.cnn.com"[/URL]
openOneAtATime[3]="[URL unfurl="true"]http://www.yahoo.com"[/URL]
openOneAtATime[4]="[URL unfurl="true"]http://www.amazon.com"[/URL]

var i=0;
for (i=0;i<openOneAtATime.length;i++) {
	WindowObjectReference = window.open(openOneAtATime[i],"window1")
	setTimeout("WindowObjectReference.close()",5000)
	}
}
</script>

----------------------------
Beau71 - MCSA
----------------------------
 
This is ad hoc.
[tt]
<script type="text/javascript">
[blue]var WindowObjectReference=null;[/blue]

function openSites() {

var openOneAtATime=new Array()[ignore]
openOneAtATime[0]="openOneAtATime[1]="openOneAtATime[2]="openOneAtATime[3]="openOneAtATime[4]="[/ignore]

var i=0;
for (i=0;i<openOneAtATime.length;i++) {
setTimeout("WindowObjectReference = window.open('"+openOneAtATime+"','window1')",20000*i);
setTimeout("WindowObjectReference.close()",20000*(i+0.8))
}
}
</script>
[/tt]
You've to give reasonable layout over time. User-agent's speed and loading time of pages can vary making the overall effect/user-surfing experience unpredictable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top