如何将参数发送到loadTexture的回调函数?

时间:2014-01-27 18:59:11

标签: javascript html5 asynchronous callback three.js

我的代码:

var textureMap = {};
var map = {"a" : 1, "b" : 2, "c" : 3};
for ( var key in map) {
   var url = "images/" + key+ ".png";
   var texture = THREE.ImageUtils.loadTexture(url, {}, function() {
        textureMap[key] = texture;
        console.log(key);    // becomes c all times
   });

}

我如何能够将密钥作为参数发送到回调函数中?因此我用3个纹理填充textureMap,而不是在同一个键上覆盖它。可能是一个非常基本的JavaScript问题,我很感激快速回答!

由于

已解决以这种方式很快:

var textureMap = {};
var map = {"a" : 1, "b" : 2, "c" : 3};
for ( var key in map) {
   var url = "images/" + key+ ".png";
   var texture = THREE.ImageUtils.loadTexture(url, {}, (function(key) {
        textureMap[key] = texture;
        console.log(key);    // outputs, a,b,c
   })(key));

}

0 个答案:

没有答案