I have a problem with my Three.js Json-Loader. I have some object their path is saved in an array.
Now I want to load them and sort them in a List so that I can select them.
But the order they are loaded is a different than they are in my array after loading because they have different size so the small one are first and the bigger are last. So after loading them I don’t know the name of the object ( the name is the path ).
My code :
for(var j=0;j<21;j++){
var path = objPath[j];
loader.load( path, function( geometry ) { save(geometry, path); } );
}
with this code the path is given to the save methode but there is always the last (objPath[20]) path because the for-loop is faster than the loading methode.
What can I do that the path is the right?
You are being bitten by a rather common mistake involving JavaScript closure rules, see this link. In short, to get the correct path passed to the save function, you need to wrap it into a helper function factory, like this: (or as shown in the above link)