Question

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, 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!!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top