Is JavaScript multithreaded?

前端 未结 9 1990
误落风尘
误落风尘 2020-11-27 04:23

Here\'s my issue - I need to dynamically download several scripts using jQuery.getScript() and execute certain JavaScript code after all the scripts were loaded, so my plan

9条回答
  •  自闭症患者
    2020-11-27 05:19

    Thought it might be interesting to try this out with a "forced", delayed script delivery ...

    1. added two available scripts from google
    2. added delayjs.php as the 2nd array element. delayjs.php sleeps for 5 seconds before delivering an empty js object.
    3. added a callback that "verifies" the existence of the expected objects from the script files.
    4. added a few js commands that are executed on the line after the GetScripts() call, to "test" sequential js commands.

    The result with the script load is as expected; the callback is triggered only after the last script has loaded. What surprised me was that the js commands that followed the GetScripts() call triggered without the need to wait for the last script to load. I was under the impression that no js commands would be executed while the browser was waiting on a js script to load ...

    var scripts = [];
    scripts.push('http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js');
    scripts.push('http://localhost/delayjs.php');
    scripts.push('http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js');
    
    
    function logem() {
        console.log(typeof Prototype);
        console.log(typeof Scriptaculous);
        console.log(typeof delayedjs);
    }
    
    GetScripts( scripts, logem );
    
    console.log('Try to do something before GetScripts finishes.\n');
    $('#testdiv').text('test content');
    
    

提交回复
热议问题