简体   繁体   中英

How do I change title of the webpage automatically for regular time intervals using javascript/jquery?

I have tried this:

for (i = 0; i < i+1; i++) {
    if (i%2 == 0) {
        document.title = "title1";
    } else {
        document.title = "title2";
    }
}

But due to the the continous/never-ending for loop the browser(Google chrome) hanged up. I'm a newbie in javascript/jquery. So, please help. Thanks for any help. PS: Sorry, Bad English.

window.setInterval(function(){
    changetitle()
},10000); //every 10 seconds

function changetitle(){
 //change title here
}

you need setInterval function.

var isOdd = true;
setInterval(function(){
    document.title= isOdd ? "title1" : "title2";
    isOdd = !isOdd;
}, 100);

something like this... function passed to setInterval will be called asynchronously in every 100 milliseconds. So your browser won't hang up.

You want to use javascript timing events: http://www.w3schools.com/js/js_timing.asp

var i = 0;
setInterval(function() {
   document.title = i++ % 2 == 0 ? "title1" : "title2";
}, 1000);

Every 1 second (1000 milliseconds) it will call this function and update the title.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM