在setInterval()中重置$ i ++

时间:2017-09-25 17:06:48

标签: javascript

我尝试用这样的jQuery重置计数器

 $i = 1;

function theTimer(startTime){

    var counter = $i++;
    var currenTime = parseInt(startTime) + counter;
    var tMin = Math.floor( parseInt(currenTime) / 60 );
    var tHour = Math.floor( parseInt(currenTime) / 3600 );

    (other stuff ...)

}

$('.btn-start-timer').click(function(startTime){
    var startTime = 320;
    banKTimer = setInterval(function() { theTimer(startTime) }, 1000);

});

$('.btn-stop-timer').click(function(){
    stoptheTimer = clearInterval(banKTimer);
});

当我重新启动计时器时,我想重置counter = $ i ++。 我忘记了一些东西,却找不到它!

1 个答案:

答案 0 :(得分:0)

$('.btn-stop-timer').click(function(){
    stoptheTimer = clearInterval(banKTimer);
    $i = 0; // Done here to allow you to get $i value for other purpose just 
            // after clearing the interval.
});