【问题标题】:jquery blinking array values in title tag标题标签中的jquery闪烁数组值
【发布时间】:2010-11-04 18:26:18
【问题描述】:

我的网站上有一个聊天功能,我想复制 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);

请帮忙!!

【问题讨论】:

    标签: jquery arrays tags title setinterval


    【解决方案1】:

    只需增加一个索引:

    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 的末尾。

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 2013-02-12
      • 2011-04-18
      • 2014-02-20
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多