user3795286
user3795286

Reputation: 138

How to use clearTimeout

I am new to javascript and Meteor and trying to clearTimeout on a function of a function.

The console states cannot find property of. I did not write the code so I am at a loss of how to stop the timer.

Any suggestions are appreciated! Code:

function startTimer(){
  start = new Date().getTime();
  time = 0;
  elapsed = '0.0';

    function instance(){
       time += 100;
       console.log('instance started');
       elapsed = Math.floor(time / 100) / 10;
       if(Math.round(elapsed) == elapsed) { elapsed += '.0'; }
          document.title = elapsed;

       var diff = (new Date().getTime() - start) - time;
       window.setTimeout(instance, (100 - diff));
    };
  window.setTimeout(instance, 100);
};

 function stopTimer() {
   clearTimeout();
 };

Upvotes: 2

Views: 1954

Answers (1)

webdeb
webdeb

Reputation: 13211

// define myTimeout somewhere globally
var myTimeout;

function instance() {
    ...
    myTimeout = setTimeout(instance, 100);
}

function stop() {
     clearTimeout(myTimeout);
}

Upvotes: 2

Related Questions