I am a newbie in javascript and what makes me so confused in the order of operations in Javascript.
lets suppose I have a following code:
var myArray = [];
function getArray(x){
var _array = [];
.... {the code here is pretty big}
return _array;
}
myArray = getArray(1);
and rest of the code are all based on myArray. Unfortunately, using this code sometimes myArray is not set in time so the rest of code doesn’t work properly. Is there anyway that I can make sure the rest of the code is executed when myArray is set properly?
Thanks,
Amir.
Place it at the top of the js file or html before any other javascript and it will execute first. This assumes you don’t have it wrapped in some sort of delay like
setTimeoutor jQuery’s document ready function.JS executes as it is loaded by the web browser, so the order it appears in determines the order it executes in relative to other JS.