As soon as the image is loaded into memory, all other objects that request this image using the same URL receive a link to the image from the browser cache. This avoids loading the same image multiple times. In the case of gif, one of the metadata that needs to be tracked is the current frame, which is not stored in the <img>
dom element, but rather at the browser level in the structure that it uses to store this gif.
When loading, this frame index is reset. Thus, although the browser processes the gif loop, the second image download sets the current frame index to the beginning, so both images are synchronized.
This is a browser implementation, and most browsers seem to follow this implementation. One of the advantages of this is that if you have thousands of small gifs (from one URL) on one page, the browser will do a lot less computation to render them, because it will only change one frame index, not thousands.
Edit: To fix your code, you will have to basically add something random at the end of your image.
function changE(x) {var image=document.getElementById (x); image.src="animated.gif?" + Math.random(); }
Therefore, the browser considers this to be a different image (i.e. a different URL).
source share