views:

18

answers:

1

What happens when I call setTimeout in a firefox extension? Is there ever a condition in which multiple callbacks can run simultaneously?

My firefox extension contains an array of URLs that I want to take screenshots of. My extension opens 5 tabs, and sets the URLs of these tabs to be the first 5 URLs in the array. Once a page finishes loading in tab N, I want to wait 30 seconds, take a screenshot, and then pop the next URL off the array and load it in tab N. The process repeats itself until the list of URLs is empty.

How can I be sure that each URL is processed exactly once? If I was writing this in java I would just synchronize all accesses to my URL list. How can I achieve this in a Firefox extension?

+3  A: 

Javascript is completely single-threaded (except for the new Workers feature, which is separate).

No two callbacks can ever run simultaneously.

SLaks
Is this true for javascript running in the browser chrome? I am running privileged javascript, as part of a firefox extension. The code isn't running inside any particular tab, and it isn't part of any HTML document.
Rob Crowell
@Rob: I'm reasonably certain that it is.
SLaks
Yes it's true for code running in the browser chrome too.
sdwilsh
@sdwilsh: Do you have a reference?
SLaks
@sdwilsh: does the single-thread rule apply for XMLHttpRequest too? If I do multiple send calls asynchronously, will more than one go out at a time, or will they queue as well?
Rob Crowell
@Rob: The requests will be sent in parallel, but the callbacks will be executed in sequence on the UI thread.
SLaks
@SLaks no, but it's the same JS engine, so it has the same constraints (just more APIs available to it).
sdwilsh