Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!