2

Is it possible to set the loop count of a GIF image using JavaScript and then publish an event when it stops playing?

For example something like:

//html code
<img src="myImage.gif" id="img1"/>

//Javascript code
var image = document.getElementById('img1');

//Image must stop playing after 3 loops
image.setLoopCount = 3;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Draco
  • 16,156
  • 23
  • 77
  • 92
  • The answer is _"No, not using JavaScript and an actual animated GIF"._ All the answers so far are helping you with alternatives, but are forgetting to provide this fundamental answer. – Phrogz Nov 17 '11 at 19:05

2 Answers2

3

here is how i would suggest doing it:

  • extract frames form gif file (you can do it online using for instace -> http://imgops.com/)
  • use javascript to change pictures, simulating animation (that way you can keep track of how many loops you have done)

Here is a jsFiddle link http://jsfiddle.net/qustosh/n5zWH/9/ I used jQuery. I did three loops and i threw a callback function at the end.

bisu
  • 31
  • 2
0

For a design side solution, you can set the GIF image to only loop a certain number of times with Photoshop. Then just use window.setTimeout(callback, milliseconds) to trigger your custom event.

You can calculate the time out from the interval used to display each frame of the animation.

Ishmael
  • 30,970
  • 4
  • 37
  • 49