1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| var message = { time: 0, title: document.title, timer: null, show: function () { var title = message.title.replace("【 】", "").replace("【新消息】", ""); message.timer = setTimeout(function () { message.time++; message.show(); if (message.time % 2 == 0) { document.title = "【新消息】" + title } else { document.title = "【 】" + title }; }, 600); return [message.timer, message.title]; }, clear: function () { clearTimeout(message.timer); document.title = message.title; } }; var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : 'mozHidden' in document ? 'mozHidden' : null; var title = document.querySelector('title'); var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange'); var onVisibilityChange = function () { if (!document[hiddenProperty]) { message.clear() } else { message.show() } } document.addEventListener(visibilityChangeEvent, onVisibilityChange);
|