473,548 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reloading different images every second

Hello,

I'd like to write JavaScript code that refreshes a frame once a second
loading a different image each time. Basically, here's what the script
would do:
0 sec load img0.gif
1 sec load img1.gif
2 sec load img2.gif
3 sec load img0.gif (back to the first image)
4 sec load img1.gif
....etc...

I do have some code that reloads the frame every second, but it always
loads the same image:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>My Frame</title>
<style type="text/css" id="mtmsheet"> </style>
</head>
<body bgcolor = "#cedfea">
<h4 align="center"> CAMERA #1</h4>
<img src="img0.jpg" name="image0" align=right></img>
<script language="javas cript">setTimeo ut("window.loca tion.reload(tru e)",1000);</script>

</body>
</html>
Any idea on how I could modify this script to load a different image
at each refresh? Any feedback is greatly appreciated.

Thanks.
SRC
Jul 23 '05 #1
3 12262
src_mag wrote:
Hello,

I'd like to write JavaScript code that refreshes a frame once a second
loading a different image each time. Basically, here's what the script
would do:
0 sec load img0.gif
1 sec load img1.gif
2 sec load img2.gif
3 sec load img0.gif (back to the first image)
4 sec load img1.gif
...etc...

I do have some code that reloads the frame every second, but it always
loads the same image:
Validate your HTML before posting, you are more likely to get answers
if what you post works except for the issue you need fixed.

<html>
No doctype. Usually not too much of a problem, but it can be the
source if errors as browsers behave differently depending on whether
they are in strict or quirks mode. In this case, they'll be in
quirks mode.
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>My Frame</title>
<style type="text/css" id="mtmsheet"> </style>
Style elements are not allowed to have id attributes.
</head>
<body bgcolor = "#cedfea">
bgcolor is depreciated, use styles to set the background colour and
always set foreground colour to something reasonable at the same time
(taking note of guidelines for visually impaired and colour
blindness).

<body style="backgrou nd-color: #cedfea; color: #330033;">

<h4 align="center"> CAMERA #1</h4>
<img src="img0.jpg" name="image0" align=right></img>
End tags on <img> elements are forbidden.

<URL:http://www.w3.org/TR/html4/struct/objects.html#ed ef-IMG>
<script language="javas cript">setTimeo ut("window.loca tion.reload(tru e)",1000);</script>
The language attribute is depreciated, use the required type
attribute.

<script type="text/javascript">

All this script does is re-load the page every second. What you seem
to want to do is change the image source attribute, so write a
function that does it.

</body>
</html>

Any idea on how I could modify this script to load a different image
at each refresh? Any feedback is greatly appreciated.


Here is a script that loops through a set of images: put the images
in the picSet array. The script assumes the images are in the same
directory as the html file, add paths if not.

The image specified in the HTML must be in the array or the script
will run every second but because it can't find it in the array,
nothing will appear to happen.

Lightly commented & tested in Firefox and IE.

<img src="01.gif" name="XX" alt="Changing image">

<script type="text/javascript">

// Array of pictures to loop through
var picSet = ['01.gif','02.gi f'];

// Function to change image
function swapImage(){
var i,x,j,n;
i = picSet.length;
j = document.images['XX'].src.split('/');
n = j[j.length-1];

// Find index of current image in array
while ( i-- && picSet[i] != n ){}

// Set new image to index + 1 or loop if beyond end of array
document.images['XX'].src = picSet[ (i+1) % picSet.length];
}

// Setup timer
setInterval("sw apImage()",1000 );
</script>

--
Rob
Jul 23 '05 #2
JRS: In article <fO************ ****@news.optus .net.au>, dated Tue, 12
Apr 2005 01:48:59, seen in news:comp.lang. javascript, RobG
<rg***@iinet.ne t.auau> posted :
All this script does is re-load the page every second. What you seem setInterval("sw apImage()",1000 );


On at least some browsers, that will not re-load *every* second. It
will re-load at intervals of one second, perhaps plus overhead, rounded
up to a timer tick.

It probably does not matter to the OP, unless his images are of a clock;
but be should know of the effect, in case it matters.

<URL:http://www.merlyn.demo n.co.uk/js-date0.htm#TaI> refers.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3
Hello Rob,

Thanks for your excellent input. I appreciate your help.

SRC

RobG <rg***@iinet.ne t.auau> wrote in message news:<fO******* *********@news. optus.net.au>.. .
No doctype. Usually not too much of a problem, but it can be the
source if errors as browsers behave differently depending on whether
they are in strict or quirks mode. In this case, they'll be in
quirks mode.
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">


....
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1652
by: aurora | last post by:
I am looking for a way for reloading updated modules in a long running server. I'm not too concerned about cascaded reload or objects already created. Just need to reload module xxx if the corresponding xxx.py is updated. I found something useful in module.__file__. Would it work if I use it to generate filenames xxx.py xxx.pyc and then...
2
1647
by: DiggidyMack69 | last post by:
Hello folks I have a rolling image script that works fine except that the images do not seem to be preloading properly. The images are still being pulled on every interval from the server according to the logs...is it because I am using the document.write method? Is that forcing a refresh? <SCRIPT language="JavaScript"...
2
5083
by: Snolly | last post by:
Hi all, Here is my issue. I have a web page (lets call it page1) with an iframe in it that then opens a pop-up window (page2). The pop-up window is used to edit some data that was loaded into page1 so I want to use onunload to reload page1 to keep the data synchronized. At first I was using just window.opener.parent.location =...
2
8509
by: Dev | last post by:
Dear Friends, I have 20 images in Array list.How do display the images into picture box every 1 second.If anyone knows please let me know....I don't want reload the form.The images are comming from network... sample code or reference links are welcome.... Thanks, Dev
2
1958
by: mikeoley | last post by:
Ok I have a Javascript slideshow working. Every image is linked to a another page in the site. Problem is...The link wont refresh to the next link once the second images rollovers in the slideshow. It only stays at the first images link. Is this a cache issue? Or is there anyway to create a random number to trick this or make it work...
61
4673
by: phil-news-nospam | last post by:
Why does SVG need a different tag than other images? IMHO, SVG should be implemented as an image type just like any other image type, allowing it to work with <img> tags, and ... here is the important part ... also work with backgrounds in other tags. I fail to see any wisdom in making SVG different than say PNG (of course the...
6
4726
by: ipy2006 | last post by:
Other than the JavaScript of timing and rotating images every few seconds or minutes, is there a way to uniquely load a different picture every time a browser open a page? Thanks, yip
7
2450
by: John Nagle | last post by:
I'm converting a web app from CGI to FCGI. The application works fine under FCGI, but it's being reloaded for every request, which makes FCGI kind of pointless. I wrote a little FCGI app which prints when the program is loaded and when it gets a request. And indeed, the program gets reloaded for each HTTP request. Something is probably...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.