开发者

jquery blinking array values in title tag

开发者 https://www.devze.com 2023-01-24 01:59 出处:网络
I have a chat feature on my website that I want to replicate the facebook functionality of blinking \"new message from John\" in the title tag when a new message comes in.I can do this for one instanc

I have a chat feature on my website that I want to replicate the facebook functionality of blinking "new message from John" in the title tag when a new message comes in. I can do this for one instance of a new message however I need to do this for all new messages (infinite possibility). Therefore a setInterval loop needs to be created and cycle through the names of people who have sent a new message. Lets assume John, Sue, George, and Katy have sent me new messages; this is what I have so far:

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

Now that I have the array "AllNames" containing all the first names of people sending me messages,开发者_StackOverflow社区 I need to cycle through this array every 1500ms and change the title tag to reflect the new name.

 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);

Please Help!!


Just increment an index:

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);

Checking against AllNames.length will prevent you from accessing past the end of AllNames.

0

精彩评论

暂无评论...
验证码 换一张
取 消