网络通知 ( javascript )
web notification ( javascript )
Q1:如何停止Interval
let intervalId = setInterval(function(){
document.title = document.title == msg ? oldTitle : msg}, 1000);
然后回到原始选项卡标题?
我试试
if (window.focus()) { clearInterval(intervalId); }
但这不起作用?
Q2:如何关注window中的特定选项卡,除了window.focus()
我还能用什么
Q3:在新 window 中打开 link 或如果已经打开则关注它
注意:window.open('').focus()
总是打开新标签,即使已经打开一个标签
请提前多多指教,谢谢
所以我假设你想在用户不在 window 时来回切换不同的标题,然后在用户回来时设置为原来的 window。
对于第 1 季度,我认为您应该监听 blur 和 focus 事件来实现功能。
let intervalId = null;
let original = 'original title';
let newMsg = 'new message';
// clear the timer when the user comes back
window.addEventListener('focus', function() {
clearInterval(intervalId);
}, false);
// set the timer when the user goes away
window.addEventListener('blur', function() {
intervalId = setInterval(function() {
document.title = document.title === original ? newMsg : original;
}, 1000)
}, false);
`
对于第 2 季度,引用自 MDN
It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.
对于Q3,window.open()也接受一个windowName
参数,当你打开window时你应该给[=14相同的window名称=] 方法,所以当打开相同的 window 时,它可以被聚焦。
window.open('url','windowName').focus()
这是一个例子:
window.open('https://www.google.com', 'google-window').focus()
Q1:如何停止Interval
let intervalId = setInterval(function(){
document.title = document.title == msg ? oldTitle : msg}, 1000);
然后回到原始选项卡标题? 我试试
if (window.focus()) { clearInterval(intervalId); }
但这不起作用?
Q2:如何关注window中的特定选项卡,除了window.focus()
Q3:在新 window 中打开 link 或如果已经打开则关注它
注意:window.open('').focus()
总是打开新标签,即使已经打开一个标签
请提前多多指教,谢谢
所以我假设你想在用户不在 window 时来回切换不同的标题,然后在用户回来时设置为原来的 window。
对于第 1 季度,我认为您应该监听 blur 和 focus 事件来实现功能。
let intervalId = null;
let original = 'original title';
let newMsg = 'new message';
// clear the timer when the user comes back
window.addEventListener('focus', function() {
clearInterval(intervalId);
}, false);
// set the timer when the user goes away
window.addEventListener('blur', function() {
intervalId = setInterval(function() {
document.title = document.title === original ? newMsg : original;
}, 1000)
}, false);
`
对于第 2 季度,引用自 MDN
It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.
对于Q3,window.open()也接受一个windowName
参数,当你打开window时你应该给[=14相同的window名称=] 方法,所以当打开相同的 window 时,它可以被聚焦。
window.open('url','windowName').focus()
这是一个例子:
window.open('https://www.google.com', 'google-window').focus()