如何满足条件时重置计数?

时间:2015-01-06 04:43:57

标签: javascript jquery

当每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();

1 个答案:

答案 0 :(得分:0)

您的代码有一些问题,特别是setInterval函数。第一个参数应该是一个函数对象, NOT 函数的调用:

应该是这样的:

setInterval( condition, 2000 );