473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why doesn't this animated jpg work properly?

I'm afraid I'm a newbie to javascript coding, so I hope this isn't too silly
a question. Basically I'm trying to display a simple animation of a sequence
of 24 hourly rain radar pictures (radar1.jpg, radar2.jpg etc). I'm
preloading the pictures into an array, then displaying the pictures in
sequence using setTimeout to call the animate() function to load the next in
the sequence as each one is displayed. Nothing clever, and it's no more than
my adaptation of a couple of similar examples I found on the net.

Trouble is, instead of loading the pictures once and displaying them from
this cache, it loads them all at the start, then reloads them again each
time they are actually displayed. Can anyone see if I've done something
wrong or can explain what is happening please? Sorry the code is not very
elegant.

Code follows >>>>>>

<title>24 hour rain radar</title>

<script language="Javascript">

Countdown = 24;

Weatherpic = new Array();
for(i = 24; i >= 0; i--) {
Weatherpic[i] = new Image() ;
Weatherpic[i].src = "url/radar" + i + ".jpg";
}
function animate() {
document.animation.src = Weatherpic[Countdown].src;
Countdown--;
if(Countdown <= 0) {
Countdown = 24;
}
}

</script>

</head>
<body>

<img name="animation" src="url/radar1.jpg"
onLoad="setTimeout('animate()',200)">

</body>
Jul 23 '05 #1
2 1944
Malcolm wrote on 03 sep 2004 in comp.lang.javascript:
function animate() {
document.animation.src = Weatherpic[Countdown].src;
Countdown--;
if(Countdown <= 0) {
Countdown = 24;
}
}

</script>

</head>
<body>

<img name="animation" src="url/radar1.jpg"
onLoad="setTimeout('animate()',200)">

</body>


Try:

var Countdown=24

function animate() {
document.animation.src = Weatherpic[Countdown].src;
Countdown--;
if(Countdown < 0) Countdown = 24;
setTimeout('animate()',200)"
}

....

<body onLoad='animate()'>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
On Fri, 3 Sep 2004 20:04:29 +0100, Malcolm <ma*****@wrights.me.uk> wrote:
I'm afraid I'm a newbie to javascript coding, so I hope this isn't too
silly a question. Basically I'm trying to display a simple animation of
a sequence of 24 hourly rain radar pictures (radar1.jpg, radar2.jpg
etc). I'm preloading the pictures into an array, then displaying the
pictures in sequence using setTimeout to call the animate() function to
load the next in the sequence as each one is displayed. Nothing clever,
and it's no more than my adaptation of a couple of similar examples I
found on the net.
May I ask why you don't use an animated GIF with frame delays set to 200ms?
Trouble is, instead of loading the pictures once and displaying them
from this cache, it loads them all at the start, then reloads them again
each time they are actually displayed.
I can think of two possible reasons:

1) The images didn't have time to preload.
2) The images weren't cached due to headers or browser settings.

There might be other explanations.
<script language="Javascript">
The language attribute is deprecated in favour of the (required) type
attribute. The former should not be used any more.

<script type="text/javascript">
Countdown = 24;

Weatherpic = new Array();
for(i = 24; i >= 0; i--) {
It would be better to use 'Countdown' than a literal.

[snip]
function animate() {
document.animation.src = Weatherpic[Countdown].src;
You should use the images collection to reference the 'animation' IMG
element:

document.images['animation'].src = ...

[snip]
<img name="animation" src="url/radar1.jpg"
onLoad="setTimeout('animate()',200)">


I don't know how well the load event is supported on IMG elements. It
might be better to start the to animation when the document loads.

By the way, an alternative to Evertjan's suggestion is:

setInterval(animate, 200);

This means you don't need to keep calling setTimeout. The make sure that
the line above is executed properly add, after the animate() definition:

// animate() function
}
animate.toString = function() {return 'animate();';};

This allows user agents to use the function reference version of
setInterval if it's supported, and fallback to the string version if it's
not.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

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

Similar topics

3
by: DOK | last post by:
I'm trying to create an eBay ad. So far, I've got the page the way I want it. However, the few animated gif's I have on the page display but are no longer animated. I can get it to work in...
9
by: John J. Hughes II | last post by:
Is it possible using Image or Bitmap to build and save an animated GIF file? I am not have a problem saving the GIF file just getting the frames added to it. I did find ImageAnimator but it only...
2
by: Koen Hoorelbeke | last post by:
Hi there, I'm trying to read an animated gif from a url (for a banner-rotator). The code I have now, does read the gif (or jpg), but the result is that I only get the first frame of the animated...
3
by: Al | last post by:
Hi all... I have created an aspx page that contains an animated GIF. I am using javascript and location.replace to redirect from this page to another aspx page which takes several seconds to...
1
by: TS | last post by:
I have a page that executes a method that takes a long time and i have an iframe that displays an progress bar animated gif while the page is running. The first time you hit it, it doesn't animate....
4
by: Al_C | last post by:
It's not that I expected it to work, but can anyone explane why if I put an animated gif as the background image of a form it is no longer animated? Is there any way to actually do that? Thanks,...
5
by: Tlink | last post by:
Is it possible to activate a animated item once the form/page is submitted for processing, as I have a number of forms that take a while and the users keep submitting the page thereby restarting...
27
by: Phil | last post by:
I thought it would be nice to display some animated GIFs on some of my forms. I put a PictureBox control on a form, and loaded my GIF file in. It animates, but not properly. It seems very jerky and...
7
by: rusdyrip | last post by:
hi all i want to make Loading Form with animated gif i make like this 'do events formloading.showdialog() 'do events
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.