当用户离开含此 JS 页面窗口时触发事件更改标题内容

<script>;(function () {
var defaultTitle = document.title;
var isRollTitle = 0; // 为 1 时滚动标题
var rollTimeDelay = '500'; // 滚动间隔
var step = 0;
var titles = [" 牛逼 "," 武汉加油!"," 爱我 "]; // 随机语
var showStyle = 'random';
// 滚动标题
if (isRollTitle) setInterval(function() { document.title = document.title.substring(1, document.title.length) + document.title.substring(0, 1);
}, rollTimeDelay);
// 离开页面
document.addEventListener('visibilitychange', function () { if (document.visibilityState == 'hidden') { switch (showStyle) {
case "random":
step = parseInt(Math.random() * titles.length);
break;
default:
case "order":
if (step >= titles.length) step = 0;
break;
}
document.title = titles[step];
step++;
} else {
document.title = defaultTitle;
}
});})();</script>