I have a javascript chat. When a user receives a message, I want the title to blink until it becomes active. (like Gmail Talk)
For example:
My website
My website
and User says: bla bla
My website
How can I achieve that using jQuery ?
What i tried so far: (blinking never stop playing)
var isOldTitle = true;
var oldTitle = "oldTitle";
var newTitle = "newTitle";
function changeTitle() {
document.title = isOldTitle ? oldTitle : newTitle;
isOldTitle = !isOldTitle;
setTimeout(changeTitle, 700);
}
changeTitle();
Full solution:
var isOldTitle = true;
var oldTitle = "oldTitle";
var newTitle = "newTitle";
var interval = null;
function changeTitle() {
document.title = isOldTitle ? oldTitle : newTitle;
isOldTitle = !isOldTitle;
}
interval = setInterval(changeTitle, 700);
$(window).focus(function () {
clearInterval(interval);
$("title").text(oldTitle);
});
Pinouchon's answer works but if I had to add an interval check so it didn't speed up the title changing when one person messaged multiple times in a row. So I had
if(timeoutId)
{
clearInterval(interval);
}
interval = setInterval(changeTitle, 700);
Basically if the interval had already been set, clear it and then reset it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With