2

Is it possible to count how many times an animated gif has played with javascript/jquery?

Spudley
  • 166,037
  • 39
  • 233
  • 307
Mobilpadde
  • 1,871
  • 3
  • 22
  • 29

2 Answers2

4

Nope, that's not possible.

However, you could create an interval using setInterval with the duration of the animation which increases a counter.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
3

As @ThiefMaster says, You can't do this with a GIF anim.

However, you can achieve what you want using a javascript animation.

Rather than saving the frames of the animation in a GIF anim, save them in a single PNG file, in a row (ie so it looks like a reel of film) either horizontally or vertically.

Display the image in an element on the page that is sized so you can only see one frame of the animation at a time, and then use Javascript to adjust the CSS offset of the image at regular intervals using setInterval or setTimeout.

This technique is known as CSS Sprite animations. It's easy to do, and it's basically the standard way of doing spot graphic animations on the web now (GIF anims are soooo 1998).

Google will give you plenty of resources to help you find out more: https://www.google.com/search?q=css+sprite+animations

You might also want to read the accepted answer to this question: Why not animated GIF instead of animated CSS sprites?

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Don't forget about SVG animations, which can use javascript directly on the SVG DOM, but also support a declarative style. http://www.w3.org/TR/SVG/animate.html – sehe May 22 '12 at 23:08
  • @sehe - good point. However for this question I'd stick with CSS sprites, as they'll be easier to convert from an existing animGIF. – Spudley May 22 '12 at 23:12