[JavaScript] javascript带回调函数的异步脚本载入程序 →→→→→进入此内容的聊天室

来自 , 2019-11-29, 写在 JavaScript, 查看 97 次.
URL http://www.code666.cn/view/94cb02fe
  1. var Loader = function () { }
  2. Loader.prototype = {
  3.     require: function (scripts, callback) {
  4.         this.loadCount      = 0;
  5.         this.totalRequired  = scripts.length;
  6.         this.callback       = callback;
  7.  
  8.         for (var i = 0; i < scripts.length; i++) {
  9.             this.writeScript(scripts[i]);
  10.         }
  11.     },
  12.     loaded: function (evt) {
  13.         this.loadCount++;
  14.  
  15.         if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
  16.     },
  17.     writeScript: function (src) {
  18.         var self = this;
  19.         var s = document.createElement('script');
  20.         s.type = "text/javascript";
  21.         s.async = true;
  22.         s.src = src;
  23.         s.addEventListener('load', function (e) { self.loaded(e); }, false);
  24.         var head = document.getElementsByTagName('head')[0];
  25.         head.appendChild(s);
  26.     }
  27. }
  28.  
  29.  
  30. //javascript/4106

回复 "javascript带回调函数的异步脚本载入程序"

这儿你可以回复上面这条便签

captcha