を貼り付けます (この段落を編集: ページがロードされた後の Ready 関数は、私たちが考えるネイティブ JS の window.load ではありません。簡単に言うと、これは単に DOM 構造を詳細な情報については、Baidu で Google で確認できます)
_execAsyn.isRunning = true;実行する必要があるキュー内の最初の関数
func = asynQueue.shift();
// 非同期読み込みモジュールの using.fn.script 関数を呼び出し、読み込み後に実行する必要があるコールバック関数を渡します完了しました
Using.fn.script(function(){
// 実行する必要がある現在の関数
func();
// 実行する必要のある関数がなくなるまで _execAsyn を繰り返します
_execAsyn();
});
/ / キュー内に実行する必要がある関数がない場合
}else{
// _execAsyn を変更します実行ステータスを false にします
_execAsyn.isRunning = false;
}
}
This function is nothing special to explain. To put it bluntly, it just executes the functions that need to be executed one by one. Then, the only thing that needs attention is why loops are not used when operating the queue, but iteration is used. The reason is
1. There may be new functions that need to be executed at any time in the queue. If a loop is used, the latest function may not be executed because the function is always inserted at the end of the queue
2 . Using.fn.script is asynchronous. If it is a loop, the current function has not been executed yet, and the next function may have entered the execution state. So, in itself, the speed of executing several functions at the same time may be higher. Why do we need to limit the parallelism of multiple functions here? The reason is also very simple, because every time a function in the queue is executed, the corresponding module may need to be loaded. So if two or more functions that rely on the same module need to be executed and are executed in parallel, the same module may be loaded. Multiple times, and may cause subsequent functions to fail to execute and cause exceptions.
This is the core part of the entire UsingJS. In it, I added the Using.Class.create function, which is mentioned at the end of the javascript dynamic loading article.
Finally take a look at the page usage:
This piece of code is deliberately loaded repeatedly, and the Using package is carried out after multiple Ready events and Ready.
There is one thing that needs special attention
Using("Http");
Using.asyn(function(){
var http = new Using.Modules.Http();
http.set("xxx");
http .show();
});
// If you use
// var ht = new Using.Modules.Http();
// Using.Modules will be reported. Http is not a constructor
// The reason is that
// Any operation is asynchronous. When this sentence is executed, the module loading of Using("Http") may not be completed yet
// This is The mistake Zhongmou makes when using it for multiple friends is always thinking that everything will be fine after importing the package
// Yes, it should be like this. After importing the package, you can cite it anywhere
// But there must be a prerequisite That means the module has to be loaded
// So please write all the code in Using.asyn
Using.asyn(function(){
var h = new Using.Modules.Http ();
h.set("ooo");
h.show();
});
UsingJS Download