To keep things simple, let's just say I want the title of the website to increment 1
every second, starting at 0
. I know how to do this with JavaScript using setInterval
—it's rather straight forward. However, set interval seems to essentially stop working once I've switched to a new tab. I see websites that do this type of periodic updating when not in focus, so I know it's possible. I tried looking through their source code but couldn't figure anything out. So, how does one accomplish this?
Well it should be done through renewing the DOM element each time a second passes.
So it could be something like this. FunctionOne(){ setInterval(function(){ var title = document.getElementByTagName('title'); var TimesChanging = "My number or variable"; for(var a = 0; a <= TimesChanging; a++) { title.innerHTML = "My title " + a; } return a; } ,1000)}
In this way each second the cicle updates the title up to a number of times decided by the TimesChanging variable..
And for the problem of not working with a different tab it may depend on the focus of the page, you might say something like:
<body onfocusout="myFunctionTwo(a)" onfocus="FunctionOne()">
myFunctionTwo(a) {
setInterval(
function(){
var title = document.getElementByTagName('title');
for (a; a<= variable; a++){
title.innerHTML = "My title " + a;
}
,1000)};
in this way on the focusing out of the body you pass the parameter a to the second function and in this way you keep on cycling the variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.