SetInterval不适用于If条件

时间:2015-02-03 13:06:10

标签: javascript setinterval

  

此条件setIntervalIdMessages == 0使setInterval无效   我不知道为什么

     

这是使setInterval不起作用的所有条件

if (NewMessageCounter > 0 && setIntervalIdMessages == 0)
  

这有效if (NewMessageCounter > 0 )

     

我在crhome debug中看到setIntervalIdMessages的值为0   并且看到setInterval代码行正在执行

var setIntervalIdMessages  = 0; 

   $.ajax({
            url: '/P/GetC',
            type: "POST",           
            dataType: 'json',
            success: function (counters) {

                var NewMessageCounter = parseInt(counters.NewMessageCounter, 10);
                var NewWatchedMeCounter = parseInt(counters.NewWatchedMeCounter, 10);


                var glower = $('.glowDiv');

                if (NewMessageCounter > 0 && setIntervalIdMessages == 0) {

                    $('#UserMessagesDiv').addClass('glowDiv');

                    setIntervalIdMessages = window.setInterval(function () {
                        glower.toggleClass('active');
                    }, 500);

                }


        });

1 个答案:

答案 0 :(得分:0)

我认为your code should work fine可能会检查您的if condition if it is satisfying or not

小型演示:



  var NewMessageCounter= 4;
 var setIntervalIdMessages  = 0; 
var glower = $('.glowDiv');
if(NewMessageCounter > 0 && setIntervalIdMessages == 0){//your condition comes here
	 setIntervalIdMessages = window.setInterval(function () {
         glower.toggleClass('active');
     }, 500);

}

.active{
  color:blue;
  }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="glowDiv">
     text here
</div>
&#13;
&#13;
&#13;