Tuesday, 15 July 2014

javascript - A timer that is pausable in NodeJS? -


How do I go about creating a timer that I could stop using nodjs?

I was thinking using SetInterval / setTimeout but is it unsure that it will work as I want? (Not "pause")

Actually, I want a re-cover timer every 5 minutes (for example), but if I do a certain job, then I want to stop it , And then on any other action, Unpause.

You do not stop a timer in node. Js. Instead, you start and stop the timer.

If you want to start it again at the same time then it would have left it if it was going, you have to keep an eye on the amount of time.

Here's an idea for a little object that lets you start and stop the timer:

  timer = function (t, fn, go) {var timer, Trimming, Tea Starred; If (! T ||! FN) {new error ("time and callback function must be specified"); } TRemaining = t; If (go) {this.start (); } This.start = function () {if (timer) {tStarted = new date () GetTime (); Timer = settimeout (FN, trimmanning); }}; This.pause = function () {if (timer) {clearTimeout (timer); Timer = null; Var now = new date () GetTime (); TRemaining - = Now - tStarted; }}} // use var t = new timer (5000, function () {// it is said when a timer is done}}; T.start (); // t.pause () after some time; // t.start () after some time;  

No comments:

Post a Comment