2010-11-04 41 views
0

我在我的网站上有一个聊天功能,我希望在标题标签中闪烁显示“来自John的新消息”的Facebook功能,当有新消息进来时我可以这样做新消息的一个实例,但是我需要为所有新消息执行此操作(无限可能)。因此需要创建一个setInterval循环,并循环发送新消息的人员的姓名。假设约翰,苏,乔治和凯蒂给我发了新消息;这是我到目前为止有:jquery闪烁标题标签中的数组值

$("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); 

请帮助!

回答

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结束。