使用 JavaScript 旋转文档标题

发布于 2024-10-04 14:29:36 字数 426 浏览 2 评论 0原文

我正在寻找使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

止于盛夏 2024-10-11 14:29:42

好吧,你可以在不使用 JavaScript 库的情况下做到这一点,就像这样......

var title = document.getElementsByTagName('title');
var msg1 = "Domain.com";
var msg2 = "New Message - Domain.com"
var current;
var titleChange;

function changeTitle(){
  if(current == msg1){
   title = msg2;
   current = msg2;
  }else{ //If the current title isn't equal to the value of msg1
   title = msg1;
   current = msg1;
  }
 titleChange = setTimeout("changeTitle()", 1000);
}

function stopChangingTitle(){
 clearTimeout(titleChange);
 title = msg1;
}

我不能保证上面的代码能够工作,但它应该能够工作!

Well you can do it without using JavaScript Libraries like so...

var title = document.getElementsByTagName('title');
var msg1 = "Domain.com";
var msg2 = "New Message - Domain.com"
var current;
var titleChange;

function changeTitle(){
  if(current == msg1){
   title = msg2;
   current = msg2;
  }else{ //If the current title isn't equal to the value of msg1
   title = msg1;
   current = msg1;
  }
 titleChange = setTimeout("changeTitle()", 1000);
}

function stopChangingTitle(){
 clearTimeout(titleChange);
 title = msg1;
}

I can't guarantee that the above code will work, but it should work!

鹊巢 2024-10-11 14:29:42

这是我使用 Dr.Molle 的解决方案想到的。

function changeTitle (mytitle,count) {
    console.log(title_change);
    console.log(count);
    if (count >= 1) {
        var titles = [$('title').attr('data-title'),mytitle];
        title_change = setInterval(function(){     
             $(document).attr('title', titles[0]);
            titles.push(titles.shift());
        },
        1000);


    } else {
        clearInterval(title_change);
        //clearTimeout(title_change);
        title_change = false;
    }

        return false;

}

然而,我确实发现当我在函数外部声明 title_change 时,我似乎无法让 setInterval 停止循环。当 count >= 1 时,进程启动,但当它返回 0 时,clearInterval 和clearTimeout 不会停止change_loop。

This is what I came up with using Dr.Molle's solution.

function changeTitle (mytitle,count) {
    console.log(title_change);
    console.log(count);
    if (count >= 1) {
        var titles = [$('title').attr('data-title'),mytitle];
        title_change = setInterval(function(){     
             $(document).attr('title', titles[0]);
            titles.push(titles.shift());
        },
        1000);


    } else {
        clearInterval(title_change);
        //clearTimeout(title_change);
        title_change = false;
    }

        return false;

}

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.

姐不稀罕 2024-10-11 14:29:41
$(document).ready(function() {
        var titles=['title1','title2','title3'];

        setInterval(function()
        {     
              $(document).attr('title', titles[0]);
              titles.push(titles.shift());
        },
        2000);

    });
$(document).ready(function() {
        var titles=['title1','title2','title3'];

        setInterval(function()
        {     
              $(document).attr('title', titles[0]);
              titles.push(titles.shift());
        },
        2000);

    });
束缚m 2024-10-11 14:29:41

现在,您每 2 秒设置一次相同的新标题,即在第一次更改后您将看不到更改。您需要存储旧标题并在两种状态之间交替,显示旧标题和新标题:

var oldTitle, timerId;

function flashTitle(newTitle) {
  var state = false;
  oldTitle = document.title;  // save old title
  timerId = setInterval(flash, 2000);

  function flash() {
    // switch between old and new titles
    document.title = state ? oldTitle : newTitle;
    state = !state;
  }
}

function clearFlash() {
  clearInterval(timerId);
  document.title = oldTitle; // restore old title
}

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:

var oldTitle, timerId;

function flashTitle(newTitle) {
  var state = false;
  oldTitle = document.title;  // save old title
  timerId = setInterval(flash, 2000);

  function flash() {
    // switch between old and new titles
    document.title = state ? oldTitle : newTitle;
    state = !state;
  }
}

function clearFlash() {
  clearInterval(timerId);
  document.title = oldTitle; // restore old title
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文