views:

267

answers:

1

Hi,

As far as I understand, all JavaScript code is event-driven and executes on a single browser thread.

However, I have some JavaScript functions that are called from within a SWF object sitting on the same page. Is this code run in the same manner as regular JS code, or is it on some separate Flash thread?

If it is on a separate thread, can I use setTimeout() to get it to run on the JS events thread? e.g.:

function calledFromFlash() {
    setTimeout(doActualWork, 0);
}

function doActualWork() {
    // blah blah blah
}

Thanks, Karthik

+2  A: 

It's still on the same thread. However, for most practical purposes if you have such a long-running JavaScript that you're worried your "main" task might block the call from setTimeout, you should consider revisiting your underlying approach.

Rex M
Hi Rex,Thanks a lot for your answer. A follow-up: is this true cross-browser, and do you know any references that support it?I'm not worried about the call being blocked, I just want to make sure that there's no synchronization issues between the callback code and my regular events.
Karthik
JavaScript simply has no mechanism for coping with multiple threads. This in itself is evidence it is *not* multi-threaded. This post is not evidence per se but raises the excellent point that concerns of threading in JS are pointless: http://damienkatz.net/2006/04/how_to_create_a.html
Rex M
If you search Google for info on threading in JS, you will find a lot of blogs that say JS is threaded, but it is all based on a misunderstanding of how the JS engine works in relation to its host (the browser).
Rex M