简体   繁体   中英

Node.js setTimeout with callbacks

I'm running a node.js app and have a process that runs every 500ms. There's a lot of logic that's being done and at times, we found it could run over 500ms - this caused problems when using a setInterval.

We redesigned this to use setTimeout with a callback such as this:

    var start = function() {

            self.performProcesses(function() {
                setTimeout(function() {
                    start();
                }, 500);
            });
        }

    start();

The problem is sometimes this stops, meaning somewhere along the road a callback from performProcesses is not being hit. There are thousands of lines of code that reach multiple objects and files.

Would anyone recommend a good way to try to debug this and isolate where the break may be?

Thanks!

When I program in JavaScript I use Aptana . It is part of a collection called Appcelerator, which is pretty much Eclipse. Aptana let's you develop and debug in JavaScript, so you can StepInto, StepOver, and whatever..

Have you tried web-inspector ?

You can also try Paul Irish anim shim at loop controlling. If you call requestAnimFrame after self.performProcess , it will be called at interval of 500ms.

var requestAnimFrame = function (callback) { setTimeout(callback, 500); };

(function animloop () {
  self.performProcesses();
  requestAnimFrame(animloop);
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM