How to create the title alert effect like Facebook?

前端 未结 3 656
说谎
说谎 2020-12-08 03:24

How to create flashing title effect like facebook? Means, when you are chatting with someone and a new message is received, a title starts to switch between the original tit

相关标签:
3条回答
  • 2020-12-08 03:48

    Set an interval that switches the title every few seconds. Untested code:

    function flashTitle(pageTitle, newMessageTitle)
    {
        if (document.title == pageTitle)
        {
            document.title = newMessageTitle;
        }
        else
        {
            document.title = pageTitle;
        }
    }
    
    setInterval("flashTitle('Facebook', 'New message from John Doe!')", 800);
    
    0 讨论(0)
  • 2020-12-08 03:52
    setInterval("var x='www.WHAK.com/Packer/',y='WHAK.com/Packer/',z=document;z.title=z.title==x?y:x",900);
    
    0 讨论(0)
  • 2020-12-08 04:13

    Code:

    (function () {
    
    var original = document.title;
    var timeout;
    
    window.flashTitle = 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;
        };
    
        cancelFlashTitle(timeout);
        step();
    };
    
    window.cancelFlashTitle = function () {
        clearTimeout(timeout);
        document.title = original;
    };
    
    }());
    

    Usage:

    flashTitle("New Message from Matt Lunn");
    

    ... or...

    flashTitle("New Message from John Smith", 10); // toggles it 10 times.
    
    0 讨论(0)
提交回复
热议问题