I have an animated gif in an img tag that I start by rewriting the src attribute. The gif was created, though, to loop and I only want it to play once. Is there a way, with Javascript or jQuery, to stop an animated gif from playing more than once?
-
2vist this post http://stackoverflow.com/questions/5818003/stop-a-gif-animation-onload-on-mouseover-start-the-activation – Osama Jetawe Oct 04 '13 at 00:04
-
Can you modify the gif to turn off the "loop" option? – Mark Oct 04 '13 at 00:05
-
7This isn't really a javascript question, you should find software for editing animated gifs, and just remove the loop option. – adeneo Oct 04 '13 at 00:08
-
See these answers: http://stackoverflow.com/a/6322003/2812842 http://stackoverflow.com/a/5818049/2812842 – scrowler Oct 04 '13 at 00:19
-
re-save it in photoshop with `save-for-web` and choose `once` under looping options at the bottom right area – vsync Sep 29 '14 at 14:57
-
Check out LunaPic, you can upload a GIF and using this web application, goto Animation > Edit Animation and select "Loop 1 Time" under "Looping". – twknab Feb 25 '19 at 06:36
-
This might help: https://slbkbs.org/jsgif/ – yunzen Mar 03 '20 at 07:45
7 Answers
I was having the same problem with an animated gif. The solution is rather simple.
Open the Animated gif in Photoshop.
Go to the Window tab and select timeline(if the timeline is not already open).
At the bottom of the timeline panel, you will find an option, which says "Forever". Change that to "Once".
Go to File> Export> Export for Web and save it as a gif.
That should do it.

- 399
- 4
- 3
-
1Seems that OP allows users to add gifs, so propably it's not an option to use Photoshop here – barbsan Feb 21 '19 at 12:24
-
4
-
1
can you find out how long the gif takes to loop once? if so then you can stop the image like this:
pseudocode:
wait until the end of the image (when it is about to loop)
create a canvas element that has a static version of the gif as currently displayed drawn on it
hide gif
display canvas element in a way that makes it look like the gif froze
javascript:
var c = $("canvas")[0];
var w = c.width;
var h = c.height;
var img = $("img")[0];
setTimeout(function () {
c.getContext('2d').drawImage(img, 0, 0, w, h);
$(img).hide();
$(c).show();
},10000);
edit: I forgot to add reference to the original answer that I took this from, sorry
Stopping GIF Animation Programmatically
that one doesn't address the time factor you need for only one loop
Also, it has been mentioned that this approach is problamatic in certain cases (It actually didn't work when I try it in firefox right now...). so here are a few alternatives:
mentioned by Mark: edit the gif itself to avoid looping. this is the best option if you can. but I've run into cases where it was not an option (like automated generation of images by a third party)
instead of rendering the static image with canvas, keep a static image version and switch to stop looping . this probablyhas most of the problems as the canvas thing
-
4Wouldn't it be simpler to replace IMG's SRC with the static image? – Yuriy Galanter Oct 04 '13 at 00:28
-
1The amount of time it takes to display all frames of the gif will be variable, a slow connection for example will take much longer. – Snixtor Oct 04 '13 at 00:29
-
@YuriyGalanter yes, it would be simpler. It would be even simpler to just get a non-looping version of the gif. but I already saw comments of those options, so I offered this solution, to avoid repetition – GuiDocs Oct 04 '13 at 00:34
Based on this answer, it's kinda expensive, but it works. Let's say a single loop takes 2 seconds. At a setTimeout after 2 seconds kick in a setInterval, that would reset image source every millisecond:
setTimeout(function() {
setInterval(function() {
$('#img1').attr('src',$('#img1').attr('src'))
},1)
}, 2000)
again, probably just a proof of concept, but here's demo: http://jsfiddle.net/MEaWP/2/

- 1
- 1

- 38,833
- 15
- 69
- 136
Actually it is possible to make a gif to stop after just one iteration or any specific number of iterations, see an example below (if it is not already stopped), or in jsfiddle.
To do that the gif must be created with number of iterations specified. This could be done using Screen to Gif, it allows to open a gif or a bunch of images and edit it frame by frame.
This solution also allows you to reset the animation by imgElem.src = imgElem.src;
but this does not work in MS IE/Edge.

- 5,420
- 3
- 23
- 37
-
-
1@ddor254 it is File menu -> Save as … -> Gif - Gif options panel. Uncheck repeat forever and set Repeat count. – Jurijs Kovzels Jul 10 '18 at 14:31
Jurijs Kovzels's answer works in some condition but not in all.
This is browser-dependent.
It works well with Firefox. But In Google Chrome and Safari, it does not work if the gif is on the same server. The example he provided works because the gif is on the external server.
To restart gifs stored on the internal server, using Google Chrome and Safari, you need extra steps to make it work.
const img = document.getElementById("gif");
img.style = "display: none;";
img.style = "display: block;";
setTimeout(() => {
img.src = img.src;
}, 0);
This is inspired by this answer.

- 121
- 7
Not sure if this is the best way to respond to everyone and have it appear after all the previous answers and comments, but it seems to work.
I don't have much control over the gif. People post whatever gif they want as the "thankyou.gif in their account directory and then the ThankYou code runs whatever they've put there when a comment is submitted to a form they've posted. So some may loop, some may not, some may be short, some may be long. The solution I've come to is to tell people to make them 5 seconds, because that's when I'm going to fade them out, and I don't care if they loop or not.
Thanks for all the ideas.

- 4,534
- 9
- 52
- 110
I know I am pretty late here but..here it is...
I don't know if you would go to this length but let me share a trick.
Open the GIF in Macromedia Flash 8(it has deprecated since then), Export the GIF as Animated GIF. You will have to choose the file location. After that you would receive a dialog box with settings. In that, add the number of times you want the animation to happen. Click OK. Problem solved.
-
1are there any non-sketchy versions of macromedia flash 8 around? Surely there are other free gif tools around these days? – Tim Ogilvy Jun 24 '18 at 16:27
-
Yes. I totally agree with that. But Macromedia Provides proper and professional tools to work on GIFs. Besides, I remember that the main aim of Macromedia Flash was to edit such fancy elements for websites. – Mukund Iyer Jun 25 '18 at 17:37