Is using setInterval ok in Node apps?

2,569 views
Skip to first unread message

Wes Gamble

unread,
Sep 23, 2011, 6:27:47 PM9/23/11
to nod...@googlegroups.com
All,

I am a noob to Node.

I noticed a pattern in the app that I'm working on (inherited) where the first invocation of a function happens through setTimeout (e.g setTimeout(x, 5000) and then the (invoked) function itself does whatever and then calls setTimeout on itself again (setTimeout(x, 5000).

I decided to replace this with one call to setInterval on the function like so: setInterval(x, 5000). But I don't see it getting re-executed. I did some Googling and I can't find anything that says not to use setInterval.

Is it ok to use setInterval inside of a Node app?

Thanks,
Wes

Jann Horn

unread,
Sep 24, 2011, 1:12:00 PM9/24/11
to nod...@googlegroups.com
2011/9/24 Wes Gamble <we...@att.net>:

> Is it ok to use setInterval inside of a Node app?

Yes.


$ node
> setInterval(function(){console.log("pong")},1000)
{ repeat: 1, callback: [Function] }
> pong
pong
pong
pong
pong
pong

Branko Vukelić

unread,
Sep 24, 2011, 2:11:14 PM9/24/11
to nod...@googlegroups.com
On 2011-09-23 17:27 -0500, Wes Gamble wrote:
> SNIPPAGE

>
> I decided to replace this with one call to setInterval on the function like so:

That's not quite the same thing, though. If you do a setTimeout, and
call another setTimeout from within the callback, you have to account
for the time it takes to execute the code up to the second invocation of
setTimeout. e.g.:

function func() {
// Simulating code that takes 200ms to execute:
setTimeout(function() {
console.log('Done');
setTimeout(func, 5000);
}, 200);
}

setTimeout(func, 5000);

This code is executed approximately every 5200ms after the first 5000ms
(unless I have a bug in there), because whatever the callback itself
takes 200ms to execute. Of course, you can imagine what happens if the
200ms is some random time frame.

> setInterval(x, 5000). But I don't see it getting re-executed. I did some
> Googling and I can't find anything that says not to use setInterval.

I don't see why x would not execute repeatedly unless x has a bug in it.
In the app I'm working on, I actually use it to start a background
process that keeps on troughout the uptime of the app.

--
Branko Vukelic
bra...@brankovukelic.com
bra...@herdhound.com

IDEA MACHINE
www.brankovukelic.com

Lead Developer
Herd Hound (tm) - Travel that doesn't bite
www.herdhound.com

Love coffee? You might love Loveffee, too.
loveffee.appspot.com

Wesley Gamble

unread,
Sep 24, 2011, 4:40:45 PM9/24/11
to nod...@googlegroups.com
My error was pretty simple - I forgot to put the function call inside of an anonymous function since I was passing a parameter to it.

Thx for the help.

W

Sent from my iPhone

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Sarfaraz Farooqui

unread,
Sep 29, 2011, 9:11:42 PM9/29/11
to nod...@googlegroups.com
I am using setInterval @ 1/10th of a second to broadcast Stock Data to all connected Clients.
 
 
 

Egor Egorov

unread,
Sep 30, 2011, 2:35:17 PM9/30/11
to nod...@googlegroups.com
No HFT trading, huh? :) 

Reply all
Reply to author
Forward
0 new messages