7

Why does Firefox randomly stop loading the <script> tag added dynamically with js?
On this picture, I load dynamically these scripts and I add them to the dom

  • "/assets/js/lib/socket.io-1.3.6.js"
  • "/assets/js/lib/tweenmax.min.js"
  • "/assets/js/lib.js"
  • "/assets/js/module.js"
  • "/assets/js/modules"

Quite randomly, the result is this, a big lag between a random script loaded dynamically and the rest of the scripts ( between 7-15s )

Pic of firebug which show the timeline loading ressources

I actually load my scripts like that

function(url, callback){
    var elem = document.createElement("script");
    elem.async = true;
    elem.src = url;
    elem.type = "text/javascript";
    elem.onload = callback;
    document.getElementsByTagName("body")[0].appendChild(elem);
}

EDIT:
When I add scripts tags in my html page, the lag doesn't appear, it only appears when I load the scripts with JavaScript. But I actually need to load these scripts with JavaScript.

There is a fiddle of the bug https://jsfiddle.net/ccgb0hqr/ If the alert show up instantly refresh the page until the bug happens

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Banou26
  • 129
  • 1
  • 12
  • What do you mean by tags?? – An0nC0d3r Dec 23 '15 at 15:59
  • Is there lots of processing between the loading of the page and the running of the function which loads your dynamic scripts? – SoluableNonagon Dec 23 '15 at 16:00
  • Not sure I understand correcly, but you're running the callback every time a script has loaded, If the callback takes long, it will postpone other stuff loaded this way. If possible, run the callbacks once all scripts have loaded? – Shilly Dec 23 '15 at 16:01
  • @AdamJeffers by tags in mean the – Banou26 Dec 23 '15 at 16:06
  • @Shilly actually actually i run this on my computer, so the callback should take at least 5ms, and this work perfectly on chrome, but on firefox, i dont know why this lag appear... – Banou26 Dec 23 '15 at 16:09
  • @SoluableNonagon what do you mean processing between the loading of the page & the scripts? i actually get the page, i init my app(loading the scripts), and i register some jquery events, thats all – Banou26 Dec 23 '15 at 16:12
  • Try loading all your scripts with that function and see if lag exists – SoluableNonagon Dec 23 '15 at 16:17
  • @SoluableNonagon the only script that i can add is jquery, now all of my scripts are loaded dynamically(client.js excluded, this is the script wich load others scripts) with my func and the lag still randomly appear, and if you meant without, i edited my original post – Banou26 Dec 23 '15 at 16:22
  • Are you adding these scripts under document ready event? – Krishna Dec 23 '15 at 18:33
  • @Krishna i just tested it under document ready, same result :'( – Banou26 Dec 23 '15 at 18:38

2 Answers2

0

Looks like socket.IO may be taking some time to load and then firing off multiple requests which will block your subsequent requests (Firefox will handle 6 at a time I believe) which coincidentally is the same amount of requests to /socket.io/, it might also explain the intermittent nature of the bug since the other requests may get in before or after socket.io initialises.

Try excluding socket.io and/or making it the last script to load to see if that helps.

You might also want to investigate any specific socket.io bugs like this one.

Community
  • 1
  • 1
Oli B
  • 514
  • 5
  • 17
0

Looks like it was a bug from firefox. Newer versions of firefox does not have this bug

Banou26
  • 129
  • 1
  • 12