当每2秒循环一个函数时,我在尝试将计数重置为0时满足条件1或2。我将在什么时候实现重置。例如;
var timer;
var a = 'door';
var b = '';//this is set by an ajax call that fires off every second
var count = 0;
function condition(){
//condition 1
if(a == 'door' && b == 'inside' && count < 30){
$('#action').html('Person A');
count++;
//after 1 minute show 1 minute sign
} else {
$('#action').html('It\'s been 1 minute');
count = 31;
}
//condition 2
if(a == 'door' && b == 'outside' && count < 30){
$('#action').html('Person B');
count++;
//after 1 minute show 1 minute sign
} else {
$('#action').html('It\'s been 1 minute');
count = 31;
}
//condition 3
//empty div & reset count
if(b == ''){
$('#action').empty();
count = 0;
}
//set interval
clearInterval(timer);
timer = setInterval(condition, 2000);// 2 seconds x 30 = 1 minute
}
condition();
答案 0 :(得分:0)
您的代码有一些问题,特别是setInterval
函数。第一个参数应该是一个函数对象, NOT 函数的调用:
应该是这样的:
setInterval( condition, 2000 );