Add new chat messages in title

Good day to all, please help us solve this problem. There is a chat, and I want to do such a thing, when a person is sitting on other tabs (i.e. not in the chat), in

it was written how many new messages, and this counter was updated every 2 seconds. I was only able to do this: <pre><code>function updateTitle(){ window.onblur = function(){ if(notread != 0) $('title').text(notread + ' новых сообщений'); else $('title').text('ЧАТ СНУЯЭиП'); } window.onfocus = function(){ notread = 0; } } </code></pre> <p>But the problem is that the data is updated only when I leave the tab, and by itself it is not updated. Tell me how you can update it constantly, without necessarily finding a person on this tab? The analog can be seen in the Vkontakte dialogs.</p>
Author: Виталина, 2014-08-25

1 answers

var refresh_interval = setinterval(function(){ 
        if(notread != 0)
            $('title').text(notread + ' новых сообщений');
        else
            $('title').text('ЧАТ СНУЯЭиП');},1000);//1000мс = 1секунда

//Чтобы остановить обновление
clearinterval(refresh_interval);
 2
Author: knes, 2014-08-25 12:11:29