Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to cleartimeout in node.js

Hi We are developing application in node.js , socket.io , and redis.

we have this procedure :

exports.processRequest = function (request,result) {
     var self = this;
     var timerknock;
     switch(request._command) {
    case 'some command': // user login with username 
            // some statement 
            timerknock=setTimeout(function() {
                //some  statemetn
            },20*1000);
        case 'other command ':
            // some statement    
            clearTimeout(timerknock);
      }
};

but when it cancel the timer it is not getting canceled when other command is executed , what should i do to cancel the timer ?

like image 929
XMen Avatar asked Jun 18 '11 07:06

XMen


People also ask

What is clearTimeout in NodeJS?

In Node, a function called setTimeout allows us to schedule a function call with a delay of a specific number of milliseconds. The clearTimeout function is used to revoke the effect of the setTimeout function.

How do you use clearTimeout?

To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.

How do I use clearInterval and clearTimeout?

The clearTimeout() method clears the time out that has been previously set by the setTimeout() function. The clearInterval() method clears the interval which has been previously set by the setInterval() function.

Do we need to clearTimeout JS?

You don't actually need to use clearTimeout , you only use it if you wish to cancel the timeout you already set before it happens. It's usually more practical to use clearInterval with setInterval because setInterval usually runs indefinitely.


1 Answers

Looks like you don't have break statements, which is going to cause problems (when you try and clear the timer it will make a new timer and clear it, but the old one will still run). Maybe that's a typo.

Your main problem is you're storing the timer "reference" in a local variable. That needs to be either enclosed or global, otherwise when you execute the function to clear the variable, timerknock has lost its value and will try and clearTimeout(undefined) which is of course, useless. I suggest a simple closure:

exports.processRequest = (function(){
   var timerknock;
   return function (request,result) {
      var self = this;
      switch(request._command) {
      case 'some command': // user login with username 
         // some statement 
         timerknock=setTimeout(function() {
            //some  statemetn
         },20*1000);
      case 'other command ':
         // some statement    
         clearTimeout(timerknock);
      }
   };
})();

Be aware that this too is a very simplistic approach, and if you set a timer before the current one has finished executing then you lose the reference to that timer. This might not be a problem for you, although you might try to implement this a little differently, with an object/array of timer references.

like image 187
davin Avatar answered Oct 03 '22 01:10

davin