egret_require.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2014-2015, Egret Technology Inc.
  4. // All rights reserved.
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // * Neither the name of the Egret nor the
  14. // names of its contributors may be used to endorse or promote products
  15. // derived from this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  18. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  23. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. //
  28. //////////////////////////////////////////////////////////////////////////////////////
  29. egret_h5 = {};
  30. egret_h5.prefix = "";
  31. egret_h5.loadScript = function (list, callback) {
  32. var loaded = 0;
  33. var loadNext = function () {
  34. egret_h5.loadSingleScript(egret_h5.prefix + list[loaded], function () {
  35. loaded++;
  36. if (loaded >= list.length) {
  37. callback();
  38. }
  39. else {
  40. loadNext();
  41. }
  42. })
  43. };
  44. loadNext();
  45. };
  46. egret_h5.loadSingleScript = function (src, callback) {
  47. var s = document.createElement('script');
  48. if (s.hasOwnProperty("async")) {
  49. s.async = false;
  50. }
  51. s.src = src;
  52. s.addEventListener('load', function () {
  53. s.parentNode.removeChild(s);
  54. this.removeEventListener('load', arguments.callee, false);
  55. callback();
  56. }, false);
  57. document.body.appendChild(s);
  58. };
  59. egret_h5.preloadScript = function (list, prefix) {
  60. if (!egret_h5.preloadList) {
  61. egret_h5.preloadList = [];
  62. }
  63. egret_h5.preloadList = egret_h5.preloadList.concat(list.map(function (item) {
  64. return prefix + item;
  65. }))
  66. };
  67. egret_h5.startLoading = function () {
  68. var list = egret_h5.preloadList;
  69. egret_h5.loadScript(list, egret_h5.startGame);
  70. };