0

我的网站上有一个聊天功能,当有新消息进来时,我想复制 Facebook 的功能,即在标题标签中闪烁“来自 John 的新消息”。我可以为新消息的一个实例执行此操作,但是我需要对所有新消息执行此操作(无限可能)。因此,需要创建一个 setInterval 循环,并在发送新消息的人的姓名之间循环。假设 John、Sue、George 和 Katy 给我发了新信息;这就是我到目前为止所拥有的:

 $("div .labels").each(function(){   //.labels where each persons name is displayed in the bottom banner bar
  var senderFirstName = $(this).attr('rel');
  //this is where I need to create the array "AllNames" containing all of the sender names
 });

现在我有了包含所有向我发送消息的人的名字的数组“AllNames”,我需要每 1500 毫秒循环一次该数组并更改标题标签以反映新名称。

 var BlinkTitle = setInterval(function(){
     $("title").text("message from " + AllNames[0]); //AllNames array needs to cycle through the array values every time the interval loops.
 },1500);

请帮忙!!

4

1 回答 1

2

只需增加一个索引:

var AllNames = ['Me', 'Myself', 'Irene'];

var ix = 0;

var BlinkTitle = setInterval(function(){
    if (++ix >= AllNames.length) ix = 0;

    $("title").text("message from " + AllNames[ix]); //AllNames array needs to cycle through the array values every time the interval loops.
},1500);

检查 AllNames.length 将阻止您访问超过 AllNames 的末尾。

于 2010-11-04T18:47:54.910 回答