使用 JavaScript 旋转文档标题
我正在寻找使用 JS 旋转网页文档标题。例如:
将 Domain.com
更改为 New Messgae - Domain.com
,然后返回 Domain.com 等等,这与 Facebook 的做法非常相似使用聊天时收到新消息。
我研究过使用 SetInterval 但它只除了一个表达式?是否可以更改此设置,或者使用 SetInterval 是错误的函数?
$(document).ready(function () {
setInterval(function () {
$(document).attr('title', 'New Message — Domain.com');
},
2000);
});
I'm looking to rotate a web page document title using JS. So for example:
Change to Domain.com
to New Messgae - Domain.com
and then back to Domain.com and so on, very similar to how Facebook does it with the new messages when using chat.
I have looked into using SetInterval but it only excepts one expression? Is it possible to change this or is using SetInterval the wrong function to use?
$(document).ready(function () {
setInterval(function () {
$(document).attr('title', 'New Message — Domain.com');
},
2000);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,你可以在不使用 JavaScript 库的情况下做到这一点,就像这样......
我不能保证上面的代码能够工作,但它应该能够工作!
Well you can do it without using JavaScript Libraries like so...
I can't guarantee that the above code will work, but it should work!
这是我使用 Dr.Molle 的解决方案想到的。
然而,我确实发现当我在函数外部声明 title_change 时,我似乎无法让 setInterval 停止循环。当 count >= 1 时,进程启动,但当它返回 0 时,clearInterval 和clearTimeout 不会停止change_loop。
This is what I came up with using Dr.Molle's solution.
I did find however when I declared title_change outside the function that I couldn't seem to get the setInterval to stop cycling around. When count comes though >= 1 the process is started but when it comes back as 0 the clearInterval and clearTimeout didn't stop the change_loop.
现在,您每 2 秒设置一次相同的新标题,即在第一次更改后您将看不到更改。您需要存储旧标题并在两种状态之间交替,显示旧标题和新标题:
Right now, you keep setting the same new title every 2 seconds, i.e. you won't see a change after the first change. You need to store the old title and alternate between two states, displaying the old and new titles: