此条件
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);
}
});
答案 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;