I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems that the execution time of the for loop is faster than the AJAX call I’m making can respond, so the order of final output is not correct sometimes. How can I solve this problem?
function LoadTempMovieList(){
var obList = [];
if(sessionStorage.struct != null){
alert(sessionStorage.struct);
obList = sessionStorage.struct.split(",");
for(var i=0; i<obList.length;i++){
MovieLoader(obList[i],"movie");
//it use setTimeOut(), the problem also present
}
}
}
update
function MovieLoader(name,type,movieArray){
$.ajax({
...
data:{shortName:name,type:type},
dataType:'html',
success:function (html){
if(html!="0"){
...
}else{
...
}
}
});
}
I’m introducing recursion to load the objects in the order they’re in the array. I’m also introducing some code that you may not feel you need to include to verify that we’ve got an array (in case some errant other function calls this, or whatever)