JS using Ajax to load JS, display loading progress, in strict accordance with the sequence of JS loaded into the page

Source: Internet
Author: User

JS using Ajax loading JS, display loading progress, in strict accordance with the sequence of JS loaded into the page,
Do mobile phone development, found a problem, some browsers, in the case of slow network speed, JS file is not loaded, subsequent calls have begun to call, resulting in an error.
Later use of this method, to ensure that at any time is completely in accordance with the sequence of JS execution.

Source:

/** * JS using ajax loading JS, display the progress of loading, in strict accordance with the sequence of JS loaded into the page * principle: The use of Ajax asynchronous multi-threaded fast loading, each file is loaded into the completion of the loading of the array, * display to the page in the order of entry into the page completely * @param {array} files to load the file list, displayed in this order one by one added to the page * @param {fun} Funok after loading completed callback * @param {F UN} funpercent during load callback */function Ajaxloadjs (files,funok,funpercent) {var fileData = [];//record loaded file V Ar oknum = 0;   Load completed quantity//loop asynchronous Load file for (var i = 0; i < files.length; i++) {loadfile (files[i]); }//Load file function loadfile (file) {$.ajax ({url:file, DataType: "Text", Success:func              tion (data) {oknum++; Filedata[file] = data; The load was successfully written to the array loadok (file);//After successful processing}, Error:function (XMLHttpRequest, Textstatus, Errorthr                                     Own) {Console.log ("Load failed:" +file) Console.log (textstatus) console.log (Errorthrown)   }      }); }//After the successful loading of the processing function Loadok (file) {//Calculate the load progress//Here add 1, because the output JS file to the page, also takes time, plus 1 is the output JS time counted as a file//So all the files are loaded after the completion of 99%, output to the page after the completion of the 100% VA       R percent = Oknum/(files.length + 1);      if (typeof (funpercent) = = "function") funpercent (Percent, file); When loading is complete, output JS to page if (Oknum = = Files.length) {for (var i = 0; i < files.length; i++) {var script        = document.createelement (' script ');        script.innerhtml = Filedata[files[i]];      document.getElementsByTagName (' HEAD '). Item (0). appendchild (script); } if (typeof (funpercent) = = "function") funpercent (1, "all"); Completion of the output is considered to be loaded complete if (typeof (Funok) = = "function") Funok ();//Callback Completion event}}}

Examples of Use:

var files = [  ' laz_word/controllers/word_set_ctrl.js?ver=31 ',  ' laz_word/controllers/word_set_ctrl.js?ver= ];ajaxloadjs (Files,function () {    console.log ("====over======");     },function (percent,file) {   Console.log (Percent,file);});

Source: jsfun.cn
http://www.jsfun.cn/#ajaxLoadJs

  

  

JS using Ajax to load JS, display loading progress, in strict accordance with the sequence of JS loaded into the page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.