Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

setInterval cleared without a var

setInterval cleared without a var

Problem

I am using node.js to read a bunch of directories, as such async is really hurting me to keep track of it all.

I am using a Setinterval to track and report progress, however being in a loop I cannot give it a variable name without some sort of crazy object organizing.

So is it possible to just clear the interval inside of the interval when a statement is met?

setInterval(function(){
  if(list.length==count){
    clearInterval(self)
  }
},50);

Basically "count" is a tally that i increment as i scan the directories and list.length is the actual amount of items in the directory. Because the loop will complete before the directories are scanned I have to keep this outside of the actual loop.

I remember there was a way to call the callee in javascript, but even with a bit of Google I can't seem to find if its possible.

So basically I want to clear the interval without setting the interval through a variable.

Problem courtesy of: Jordan Ramstad

Solution

Would something like this work?

(function(){
    var x = setInterval(function(){
        //some if
        clearInterval(x);
    }, 50);
})();

You can scope your interval variable to the inner function.

Solution courtesy of: Kees C. Bakker

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

setInterval cleared without a var

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×