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

228
Views
JavaScript Title-Animation for inactive Tab

I found a script here to query the browser tab activity and wanted to combine that with a function that plays a small title animation when inactive.

Apart from the fact that i can still optimize the performance somewho, one error symbols are displayed on each side when the train has passed through once.

At first I thought it was the emoji (two combined emoji), but it happens to others too. Anyone have an idea?

var vis = (function () {
    var stateKey, eventKey, keys = {
        hidden: "visibilitychange",
        webkitHidden: "webkitvisibilitychange",
        mozHidden: "mozvisibilitychange",
        msHidden: "msvisibilitychange"
    };
    for (stateKey in keys) {
        if (stateKey in document) {
            eventKey = keys[stateKey];
            break;
        }
    }
    return function (c) {
        if (c) {
            document.addEventListener(eventKey, c);
            //document.addEventListener("blur", c);
            //document.addEventListener("focus", c);
        }
        return !document[stateKey];
    };
})();

var storeTitle = document.title,
    myAnimation;

function getLokTitle() {

    let title = "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ๐Ÿš‚ ",
        //let title = "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ๐Ÿš„ ",
        i = 0;

    myAnimation = setTimeout(function () {
        setInterval(function () {
            document.title = title.substr(i, title.length) + title.substr(0, i);
            i = (i + 1) % title.length;

            if (vis() === true) {
                clearTimeout(myAnimation);
                document.title = storeTitle;
                return storeTitle;
            }
        }, 250);
    }, 1000);

    return title;
}

function getStoreTitle() {

    if (typeof myAnimation !== "undefined") {
        clearTimeout(myAnimation);
    }
    return storeTitle;
}

vis(function () {
    document.title = vis() ? getStoreTitle() : getLokTitle();
    //console.log(new Date, 'visible ?', vis());
});

// to set the initial state
document.title = vis() ? getStoreTitle() : getLokTitle();
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

Thanks to Niet the Dark Absol,

here is the final code:

// Source: https://stackoverflow.com/questions/19519535/detect-if-browser-tab-is-active-or-user-has-switched-away/19519701#19519701
//  var visible = vis();
//  vis(function(){});
var vis = (function () {
    var stateKey, eventKey, keys = {
        hidden: "visibilitychange",
        webkitHidden: "webkitvisibilitychange",
        mozHidden: "mozvisibilitychange",
        msHidden: "msvisibilitychange"
    };
    for (stateKey in keys) {
        if (stateKey in document) {
            eventKey = keys[stateKey];
            break;
        }
    }
    return function (c) {
        if (c) {
            document.addEventListener(eventKey, c);
            //document.addEventListener("blur", c);
            //document.addEventListener("focus", c);
        }
        return !document[stateKey];
    };
})();

var storeTitle = document.title,
    myAnimation;

function getLokTitle() {

    var title = "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ๐Ÿš‚ ",
        //let title = "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ๐Ÿš„ ",
        i = 0;

    document.title = title;

    myAnimation = setTimeout(function () {
        let title = Array.from("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ๐Ÿš‚ ");
        setInterval(function () {
            //document.title = title.substr(i, title.length) + title.substr(0, i);
            document.title = title.slice(i).concat(title.slice(0, i)).join("");
            i = (i + 1) % title.length;

            if (vis() === true) {
                clearTimeout(myAnimation);
                document.title = storeTitle;
                return storeTitle;
            }
        }, 250);
    }, 1000);

    return title;
}

function getStoreTitle() {

    if (typeof myAnimation !== "undefined") {
        clearTimeout(myAnimation);
    }
    return storeTitle;
}

vis(function () {
    document.title = vis() ? getStoreTitle() : getLokTitle();
    //console.log(new Date, 'visible ?', vis());
});

// to set the initial state
document.title = vis() ? getStoreTitle() : getLokTitle();
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!