通过setInterval在另一个函数的函数内停止动画

时间:2016-10-16 10:19:58

标签: javascript jquery

在我的消息系统中,如果出现新消息,则调用一个函数,此函数在setInterval中通过jquery动画显示滚动。

现在我想从另一个函数停止它如何检查是否没有新消息。

我调用此函数来显示滚动:

function scroll(userID) { 
  $('.scroll'+userID).css('left','250px').show().html(''+ userID +' sent a new massage').animate({
      left: '-='+(250+149)
      }, 2000, function() {
      scrollt(userID);
  });
 window.Ccolor = setInterval(function(){$('.popup'+userID).css('background-color','red');}, 2000);
}

setInterval对新邮件的另一个功能检查

function update() {
//code get userID have no new mail but scroll is running 
//on his minimize popup

//want to stop scroll by uesrID

//Tried
clearInterval(Ccolor); //not work also can`t set userID
}

1 个答案:

答案 0 :(得分:0)

如果新消息即将发生,则在update()函数中添加一个标志,在scroll()函数中使用setTimeout而不是setInterval,如果flag设置为true则调用scroll

    function scroll(userID) { 
       $('.scroll'+userID).css('left','250px').show().html(''+ userID +' sent a new massage').animate({
      left: '-='+(250+149)
      }, 2000, function() {
      scrollt(userID);
  });
 window.Ccolor = setTimeout(function(){$('.popup'+userID).css('background-color','red');}, 2000);
}
    function update() {
    var flag=false;
    //set flag=true is new message come
    if(flag===true)
     scroll(userId);
    else
    setTimeout(update,2000);
    }

我假设你有自己的逻辑来知道userId是否是新的,或者你也可以在你的html元素中设置data- *属性

相关问题