Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
How to count how many times an animated gif loops and display that count in javascript?

Is there a way to display the number of times an animated gif loops? For example you land on a page with an animated gif and when that gif loops fully through its animation one time, a 1 is displayed. Than that number increases by 1 everytime the gif fully loops. Forever.

Thanks for any insight!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I calculate the duration time of the gif and then setInterval. My gif is 15 seconds long.

const gif = document.getElementById("gif");
    gifLoopCounterInit(gif.src);
    function gifLoopCounterInit(fileSrc) {
        var request = new XMLHttpRequest();
        request.open('GET', fileSrc, true);
        request.responseType = 'arraybuffer';
        request.addEventListener('load', async function () {
            calcDurationGif(request.response).then(function (duration) {
                const loopCounter = document.getElementById("loop-counter");
                setInterval(function () {
                    loopCounter.innerHTML++;
                }, duration * 1000);
            });
        });
        request.send();
    }
    function calcDurationGif(file) {
        return new Promise((resolve, reject) => {
            try {
                let arr = new Uint8Array(file);
                let duration = 0;
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] == 0x21
                        && arr[i + 1] == 0xF9
                        && arr[i + 2] == 0x04
                        && arr[i + 7] == 0x00) {
                        const delay = (arr[i + 5] << 8) | (arr[i + 4] & 0xFF)
                        duration += delay < 2 ? 10 : delay;
                    }
                }

                console.log(duration / 100);
                resolve(duration / 100);
            } catch (e) {
                reject(e);
            }
        });
    }
<img id="gif" src="https://c.tenor.com/AeFQKHQFFq8AAAAd/gif-art.gif" alt="">
    <p>Loops : <span id="loop-counter">0</span> </p>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda