javascript - A timer that is pausable in NodeJS? -


how go creating timer pause using nodejs?

i thinking of using setinterval/settimeout unsure if work want to? (since there's no "pause")

basically want re-occuring timer every 5 minutes (for example) if action, want pause, , on action, unpause.

you don't pause timer in node.js. instead, start , stop timer.

if want start again same amount of time remaining have had left if kept running, have keep track of amount of time.

here's idea little object lets start , stop timer:

var timer = function(t, fn, go) {     var timer, tremaining, tstarted;      if (!t || !fn) {         throw new error("must specify time , callback function");     }      tremaining = t;      if (go) {         this.start();     }      this.start = function() {         if (!timer) {             tstarted = new date().gettime();             timer = settimeout(fn, tremaining);         }     };      this.pause = function() {         if (timer) {             cleartimeout(timer);             timer = null;             var = new date().gettime();             tremaining -= - tstarted;         }     } }  // usage var t = new timer(5000, function() {     // called when timer done });  t.start();  // time later t.pause();  // time later t.start(); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -