Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash/blink tab effect in Javascript

I'm creating a simple chat in Javascript/PHP. I would like to flash/blink tab when new message is coming like on Facebook for example. How can I do that?

like image 340
Stock Overflow Avatar asked Mar 13 '23 21:03

Stock Overflow


1 Answers

Here is example code:

(function () {

var original = document.title;
var timeout;

window.coders = function (newMsg, howManyTimes) {
    function step() {
        document.title = (document.title == original) ? newMsg : original;

        if (--howManyTimes > 0) {
            timeout = setTimeout(step, 1000);
        };
    };

    howManyTimes = parseInt(howManyTimes);

    if (isNaN(howManyTimes)) {
        howManyTimes = 5;
    };

    cancelcoders(timeout);
    step();
};

window.cancelcoders = function () {
    clearTimeout(timeout);
    document.title = original;
};

}());

You can use this code something like :

coders("New Message from Bhavin Solanki");

... or...

coders("New Message from Bhavin Solanki", 20); // toggles it 20 times.
like image 88
Bhavin Solanki Avatar answered Mar 24 '23 09:03

Bhavin Solanki