Solved

Javascript animation - repeat it?

Posted on 2004-05-02
7
632 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javascript works in Chrome, but none of the other browsers 14 54
PDF Turn Look 7 38
Dropdown animation to normal dropdown 6 29
Drag & Drop Error 5 33
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

752 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