Solved

Javascript animation - repeat it?

Posted on 2004-05-02
7
624 Views
Last Modified: 2013-11-19
I have some simple javascript animation:
var counter=0
var timer
var imgs = new Array()
for(var i=0;i<2;i++) {
  imgs[i]=new Image()
  imgs[i].src=(i+1)+".png"
}

function animate() {
  counter++;
      if (counter >= imgs.length) counter=0;
  document.images["anim"].src=imgs[counter].src
  timer=setTimeout("animate()",200)
}

I want to use it several times on one page calling it this way:
<BODY onLoad="animate()">
<IMG SRC="3.png" NAME="anim">

It will work fine for one instance but if I want multiples I only get one animated and all the rest are static. For each instance I am using the same img src line. I have tried actually making multiple copies of the little java function and loading and calling them seperately but that didn't work either.

Anyone have an idea how I can get multiples of this?
0
Comment
Question by:SmartGirl
  • 3
  • 2
  • 2
7 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 10973517
They each need to have a unique name so you can loop through them:

<IMG SRC="3.png" NAME="anim0">
<IMG SRC="3.png" NAME="anim1">
<IMG SRC="3.png" NAME="anim2">

Then:

function animate() {
  counter++;
     if (counter >= imgs.length) counter=0;
     for (i=0;i<3;i++
     {
         str='anim'+i;
  document.images[str].src=imgs[counter].src
     }
  timer=setTimeout("animate()",200)
}


Cd&
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10973580
You can also have just one name and loop through them, like this:

function animate() {
  counter++;
     if (counter >= imgs.length) counter=0;
  var img=document.images["anim"];
  for(i=0;i<img.length;i++){img[i].src=imgs[counter].src;}
  timer=setTimeout("animate()",200)
}
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10973582
hmmm...or maybe not...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10973632
B?  Was there something wrong with that solution.  If you still needed more, all you had to do was ask. :^(

Cd&
0
 

Author Comment

by:SmartGirl
ID: 10973636
The code didn't compile and I had to correct it. It took a fair amount of trouble shooting to find the error. For 500 points I expect it to at least compile.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10973685
Compile?  Javascript?  

In any case if you want code corrected, post the errors; or not ...  If you want to pay my normally hourly rate, you get a guarantee of typo free code.  But as long as you are getting it for free I reckon you should be saying thank for volunteering your time to help me; but I guess some people think the rest of the world owes them something.

Cd&

0
 

Author Comment

by:SmartGirl
ID: 11354823
I gave the question the maximum points because it was urgent. I have to pay for points, they AREN'T FREE, and I expect to get my money's worth. If you don't want to put the effort into answering the question correctly don't expect to get full points.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question