0

I am trying to pass an integer value i to callback, which is not working as expected since i is available as reference.

for (var i = this.texturesPath.length - 1; i >= 0; i--) {

var textureIndex = i;


loader.load(baseTexturePath + this.texturesPath[i], function(texture) {

    scope.textures[textureIndex] = texture;
});

What is the solution/approach for this scenario?

1 Answers1

-1

This is what you need an IIFE, so you can "save" the i;

for (var i = this.texturesPath.length - 1; i >= 0; i--) {
  (function (index){
    loader.load(baseTexturePath + this.texturesPath[index], function(texture){
      scope.textures[index] = texture;
    })
  })(i)
}
AngelSalazar
  • 3,080
  • 1
  • 16
  • 22