-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesLoader.js
69 lines (51 loc) · 1.67 KB
/
filesLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Created by KurekB on 2016-11-16.
*/
var loadedResources=new Object();
function MyLoader(){
var callbackOnLoaded=function(){};
var _images;
function OnLoading(){
this.imagesToLoad=0;
this.imagesLoaded=0;
}
var onLoading=new OnLoading();
this.load=function(pathToJSON,callbackOnLoadedD){
callbackOnLoaded=callbackOnLoadedD;
loadJSON(pathToJSON,
function(res){
_images = JSON.parse(res);
loadImages();
for (var image in _images) {
onLoading.imagesToLoad++;
}
}
);
};
function loadImages(){
for(var image in _images) {
var imageObj = new Image();
loadedResources[image]=imageObj;
imageObj.onload = function () {
onLoading.imagesLoaded++;
if(onLoading.imagesLoaded===onLoading.imagesToLoad){
callbackOnLoaded();
}
}
imageObj.src = _images[image].imgPath;
}
console.log(_images, onLoading, onLoading.imagesToLoad);
};
}
function loadJSON(pathToJSON,callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', pathToJSON, true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}