Start a timer - Node.js Global

Node.js examples for Global:Timer

Description

Start a timer

Demo Code

function startTimer(time,update,complete) {
    var start = new Date().getTime();
    var stop = false;
    var interval = setInterval(function() {
        if(stop) return;
        var now = time-(new Date().getTime()-start);
        if( now <= 0) {
            clearInterval(interval);
            complete();/*from   ww w  .  ja  v  a  2  s  . c o m*/
        }
        else update(Math.floor(now/1000));
    },1000); // the smaller this number, the more accurate the timer will be

    return {
        stop : function() {
          stop = true;
        clearInterval(interval);
        }
    };
}

Related Tutorials