2010-11-04 43 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結束。