Skip to content

Instantly share code, notes, and snippets.

@towry
Created January 6, 2016 10:20
Show Gist options
  • Save towry/1dbaaa53dad85c5ca673 to your computer and use it in GitHub Desktop.
Save towry/1dbaaa53dad85c5ca673 to your computer and use it in GitHub Desktop.
var __on = false;
var vis = (function(){
var stateKey, eventKey, keys = {
hidden: "visibilitychange",
webkitHidden: "webkitvisibilitychange",
mozHidden: "mozvisibilitychange",
msHidden: "msvisibilitychange"
};
for (stateKey in keys) {
if (stateKey in document) {
eventKey = keys[stateKey];
break;
}
}
return function(c) {
if (c) document.addEventListener(eventKey, c);
return !document[stateKey];
}
})();
var or = document.title;
vis(function () {
if (!vis()) {
__on = true;
changeTitle();
} else {
__on = false;
document.title = or;
}
})
function changeTitle () {
if (!__on) return;
var a = '你有新消息';
if (document.title == a) {
document.title = or;
} else {
document.title = a;
}
setTimeout(changeTitle, 500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment