delaying output

This is a discussion on "delaying output" within the JavaScript Forum section. This forum, and the thread "delaying output are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 18th, 2007, 03:31
New Member
Join Date: Sep 2007
Location: Canada
Age: 24
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
delaying output

I'm trying to make a webpage that will open a number of images at the start, but there needs to be a pause between one image and the next. This is the code I wrote (well, the only difference is that this one is around 150 images short):
Code: Select all
<html>
<head>
</head>
<body>
<script type="text/javascript">
var address = new Array();
address[0]="<img src='a.jpg' alt='a' />";
address[1]="<img src='b.jpg' alt='b' />";
address[2]="<img src='c.jpg' alt='c' />";
address[3]="<img src='d.jpg' alt='d' />";
address[4]="<img src='e.jpg' alt='e' />";
address[5]="<img src='f.jpg' alt='f' />";

for(i=0; i<=5; i++)
{
   setTimeout (document.write(address[i]), (500*i)+1000)
}
</script>

</body>
</html>
After running this, only the first image is displayed on screen. Please help if at all possible.

Last edited by karinne; Sep 18th, 2007 at 15:34. Reason: Please use the vBcode [ code ] when inserting code in your post.
Reply With Quote

  #2 (permalink)  
Old Sep 18th, 2007, 05:38
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: delaying output

Okay so I can see a couple of problems here.

setTimeout requires a string that will be eval's when it's run so just having a statement won't work and you would be better with a function to call back rather than a for loop.

Code: Select all
<html>
<head>
</head>
<body>
<script type="text/javascript">
var address = new Array();
address[0]="<img src='a.jpg' alt='a' />";
address[1]="<img src='b.jpg' alt='b' />";
address[2]="<img src='c.jpg' alt='c' />";
address[3]="<img src='d.jpg' alt='d' />";
address[4]="<img src='e.jpg' alt='e' />";
address[5]="<img src='f.jpg' alt='f' />";

var timer;
var imgtoWrite = 0;

function imageWrite {
document.write(address[imgtoWrite]);

    imgtoWrite++;
    if (imgtoWrite < address.length)
       timer = setTimeout('imageWrite()', 1500);


}
</script>

</body>
</html>
That will call the function imageWrite every 1.5 seconds until it has been through all the images in the array.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Sep 18th, 2007, 15:31
New Member
Join Date: Sep 2007
Location: Canada
Age: 24
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: delaying output

I gave it a try but nothing was output to screen
Reply With Quote
  #4 (permalink)  
Old Sep 18th, 2007, 15:57
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: delaying output

Oops, forgot to actually put the initial function call in

After

function imageWrite {
document.write(address[imgtoWrite]);

imgtoWrite++;
if (imgtoWrite < address.length)
timer = setTimeout('imageWrite()', 1500);


}

put

imageWrite();
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Sep 19th, 2007, 04:02
New Member
Join Date: Sep 2007
Location: Canada
Age: 24
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: delaying output

It works, at least a little bit. The first image is displayed, then the second, and then at the bottom left of IE it says error on page. Firefox displays the first, that blanks out, then nothing.

Last edited by theosx; Sep 19th, 2007 at 04:33.
Reply With Quote
  #6 (permalink)  
Old Sep 19th, 2007, 04:51
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: delaying output

Pretty sure it's because document.write() destroys the whole document.

You may have to try

Code: Select all
<html>
<head>
</head>
<body>
<div id="imageDiv">
</div>
<script type="text/javascript">
var address = new Array();
address[0]="<img src='a.jpg' alt='a' />";
address[1]="<img src='b.jpg' alt='b' />";
address[2]="<img src='c.jpg' alt='c' />";
address[3]="<img src='d.jpg' alt='d' />";
address[4]="<img src='e.jpg' alt='e' />";
address[5]="<img src='f.jpg' alt='f' />";

var timer;
var imgtoWrite = 0;

function imageWrite() {
document.getElementById('imageDiv').innerHTML += address[imgtoWrite];
    imgtoWrite++;
    if (imgtoWrite < address.length)
       timer = setTimeout('imageWrite()', 1500);


}

imageWrite();
</script>

</body>
</html>
That will work.

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #7 (permalink)  
Old Sep 19th, 2007, 04:56
New Member
Join Date: Sep 2007
Location: Canada
Age: 24
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: delaying output

thanks, but I already fixed it. I couldn't have done it without your help.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php output in to CSS dhossai Web Page Design 1 Oct 30th, 2007 18:38
Distinct output using ASP/SQL bullrider Classic ASP 0 May 29th, 2007 16:55
displaying the output madhuri.t Classic ASP 3 Jul 12th, 2006 15:49
output IE vs. FF jojo Web Page Design 5 Dec 29th, 2005 13:35
no output at all -- can't see why in code (???) jswebdev PHP Forum 8 Oct 16th, 2005 05:07


All times are GMT. The time now is 22:42.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43