Hacker News new | past | comments | ask | show | jobs | submit login
Javascript asynchronous problems (codingjohnson.com)
6 points by gringofyx on Aug 19, 2013 | hide | past | favorite | 6 comments



I thought setTimeout was still blocking... Javascript isn't threaded, after all. When you do a setTimeout with a large interval, doesn't it simply keep track of how long it's been since you made the call, and at the appropriate time blocks the current code and runs the function? If so, what difference does using setImmediate/setTimeout(x, 0) make?


Timeouts are not checked on every event loop, hence the 4ms delay. And no, setTimeout is asynchronous and processed during the event loop.

Basic event loop is; timeouts, js execution, repaint, reflow, etc


What I mean to say is, what's the practical difference between running setImmediate(doSomething) and just calling doSomething() in its place if setImmediate blocks the execution anyway, is there some kind of special use that would make it useful?


Yes, render,reflow,timeouts,event handling, etc happens.. You might need to set a CSS property and then animate it. In this case you'll need to wait for a render in between the two steps

Also useful if you need to wait until after all event handlers are executed in the current context


Gotcha, thanks for the explanation!


that explains my issues with <10ms setTimeouts, thanks!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: