When does the browser event loop begin?

I use a framework that enables auto-connect to the server when the page loads. I can turn it off by passing parameter arguments, but the line that bothers me is this :

You can prevent this initial socket from automatically connecting by disabling io.sails.autoConnect before the expiration of the first event loop.

My questions:

  • When does the first cycle of the event loop end?
  • Is this behavior the same in all modern browsers (IE9 +)?
  • I have a bunch of scripts (in <body>) loading between lib and my input file. Does this mean when the first cycle expires? EDIT: Yes, it is.
  • How can I ensure that my code runs before the expiration of the first loop?
  • Is such an auto-join implementation a good practice?
+7
javascript browser event-loop
source share
1 answer

The documentation for the source file is a bit clearer; he says: "This can be disabled or configured by setting io.socket.options during the first cycle of the event loop."

Basically, what happens is that in the library there is a call to setTimeout(fn, 0) , which is idiomatic for starting a parallel process. However, the JS standards explicitly state that JS is single-threaded: in other words, although setTimeout and setInterval asynchronous , they are not actually parallel in the sense that any of their codes will run simultaneously with any other code. Therefore, they wait until the current function ends before they are executed. This queuing mechanism is known as the JavaScript event loop.

I believe that what you asked the script to author was to change the source to include the corresponding change, perhaps at the bottom of the file for your convenience.

It is also likely that a similar effect will be achieved by placing the <script> under the one loading this JS. This was not explicitly standardized by HTML 4, but it can be implicitly standardized in the new HTML 5 specification (this is a complex interaction between the various parts of the specifications).

In terms of HTML5, it looks like current specifications say that there is an afterscriptexecute event and a load event that occur immediately after loading any remote script (or, if it is an inline script, the load event is scheduled as a task - I'm not sure when this will happen). This way you can guarantee this without changing the script, instead:

 <script> function do_not_autoload() { /* ... */ } </script> <script onload="do_not_autoload()" src="./path/to/sails.io.js"></script> 

but I'm not sure what the compatibility table for script@onload will look like.

I made you a jsfiddle that you can use to capture a β€œfingerprint” for different browsers in order to understand what valuation orders are missing there in the wild. * is the document.body.onload event. On my system, it produces:

 Firefox 32.0.3 : cafdbe* Chrome 37.0.2062 : cafd*be IE 11.0.9600 : cafd*be 

In other words,

+2
source share

All Articles