Aboud Aboud - 1 month ago 24
Javascript Question

Loop Change Page Title JavaScript

So I have this code



function changeTitle() {
var title = document.title;
var variable = document.querySelector('.liconspan').innerHTML;
console.log(variable)
setTimeout(changeTitle, 3000);
document.title = title;
}
changeTitle();





I want to loop changing the title like the following:

"One" > "Two" > "One" > "Two" etc.. just sample texts will be replaced with the text I want and the actual title to loop and change from.

Cheers!

Answer

Something like this?

var first = true;
function setTitle()
{
    if(first)
        document.title = "One";
    else
        document.title = "Two";
    first = !first;
}
setInterval(setTitle, 3000);