Wait until the data is loaded.

so, I am using the DataManger.loadDataFile() to load map data from the map json file to an object.


The problem is: it seems( I could be wrong) this function is asynchronous and it won't allow me to do anything with this object until it is completely loaded. 


My question is if there is any way to suspend the game(or thread?) and wait until the data is loaded. Many Thanks~
well, i dig out the solution to this problem by using setInterval().

Code:
s = setInterval(checkLoaded,1000);//check if the data is available every 1sec
function checkLoaded(){
	if($dataES != null){
      clearInterval(s);
      //initialization code goes here
    }
}
You can use a callback


DreamX.WeaponArmorSoundEffects.Scene_Boot_isReady = Scene_Boot.prototype.isReady;
Scene_Boot.prototype.isReady = function () {
if (!DreamX.WeaponArmorSoundEffects.loaded
|| DreamX.WeaponArmorSoundEffects.loaded === false) {
return false;
}
return DreamX.WeaponArmorSoundEffects.Scene_Boot_isReady.call(this);
};

DreamX.WeaponArmorSoundEffects.loadSEData = function () {
this.loadData(this.SEDataFileName(), this.seLoadCallBack);
};

DreamX.WeaponArmorSoundEffects.SEDataFileName = function () {
return paramFileFolder + paramFileName + ".json";
};

DreamX.WeaponArmorSoundEffects.seLoadCallBack = function (data) {
DreamX.WeaponArmorSoundEffects.data = data;
DreamX.WeaponArmorSoundEffects.loaded = true;
};

// thanks to Iavra
DreamX.WeaponArmorSoundEffects.loadData = function (url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url);
request.onload = function () {
callback(JSON.parse(request.response));
};
request.onerror = function () {
throw new Error("There was an error loading the file '" + url + "'.");
};
request.send();
};


Here I am passing the function seLoadCallBack to the loadData function as the callback. When the data loads, it'll call this function and do something with the data.

Original thread: https://forums.rpgmakerweb.com/threads/wait-until-the-data-is-loaded.62642/