Hacker News new | ask | show | jobs
by dwj 4952 days ago
The problem is that while in a loop, the browser's event loop is not fired. You have to use setTimeout and break out of the loop. Sometimes it's not feasible to use setTimeout for various reasons, and sometimes the browser chugs along even when you do use setTimeout. Javascript is the only platform that doesn't permit you to spin the event loop (technically Android doesn't let you do it, but there are hacks which work nicely). I've just had to put a project on hold due to this (basically porting a C++ web conferencing platform to javascript using emscripten).

Screengrabbing does make sense, and is required for web conferencing. All other features are there (audio/video/canvas), screengrabbing is the only one missing.

2 comments

We're talking about Javascript the language, not individual implementations of javascript in browsers. Any browser could implement screengrab API today, they just haven't. Same for multithreaded javascript. Audio/video/canvas are all things that browsers have implemented, they are not innate features of Javascript.

Also despite what you say you CAN trigger an event (or whatever) inside a loop, it just won't do anything until you're out of the loop. This is because your browser implements javascript in a single thread. There are two easy solutions: Don't use a loop that's going to last for a long time, or use a Web Worker. This is, again, something that browsers have implemented, not a part of javascript. http://en.wikipedia.org/wiki/Web_worker

I didn't mean 'trigger an event', I meant 'spin the event loop'. Unfortunately web workers have too many limitations, and won't work in many situations. And the solution of 'don't use a loop' isn't possible/feasible in certain cases. No point in going into detail here, but basically I am porting a virtual machine to javascript.
> The problem is that while in a loop, the browser's event loop is not fired.

So by "the event loop is not fired" you mean "the event loop does not run" when you're already being run by the event loop? That seems to be... logical.

> You have to use setTimeout and break out of the loop.

You can also use e.g. window.postMessage. but yes to make the event loop run you do indeed have to yield to the event loop.

> and sometimes the browser chugs along even when you do use setTimeout.

Again, I'm not exactly clear on what you mean. Javascript has a toplevel event loop and a queue of fired events, when you yield to the event loops it runs the event queue in order, not sure what's surprising about this.

Hell, I'm not even sure this discussion makes sense as part of ECMAScript comments either, the event loop is not part of ECMAScript and it's perfectly possible to have JS runtimes with no event loops (the tracemonkey console for instance)

> Screengrabbing does make sense, and is required for web conferencing. All other features are there (audio/video/canvas), screengrabbing is the only one missing.

Reading comprehension. It happens to be a thing.

I didn't say screengrabbing made no sense, I said it made no sense as part of ECMAscript. It might make sense as part of WebRTC or as part of some DOM API, as part of ECMAScript it does not make any.