I want to change the title of my HTML page every second with JavaScript.
First the title will be "Hold on.", after one second it will be "Hold on.." and then after one second "Hold on...". After that it has to loop the same thing over and over again
I already tried this but that didn't work:
setTimeout(() => { document.title = "Hold on."; }, 1000);
setTimeout(() => { document.title = "Hold on.."; }, 1000);
setTimeout(() => { document.title = "Hold on..."; }, 1000);
I hope you can help me.
setTimeout
sets a timeout, not an interval. It means it will run only once. To set intervals you'll have to use setInterval
.
let dots = 1;
setInterval(() => {
document.title = "Hold on" + ".".repeat(dots);
dots++;
if (dots > 3) dots = 1;
}, 1000);