Javascript JS-(异步)XHR和增量

Javascript JS-(异步)XHR和增量,javascript,ajax,xmlhttprequest,blob,increment,Javascript,Ajax,Xmlhttprequest,Blob,Increment,第一篇文章是这样写的,但事实上是一个长期用户,因为我在这里找到了我需要的一切…直到现在 我在XHR方面遇到了一个棘手的问题。我可能会说我是JS的新手,但我没想到会发生这样的事情,我不能四处走动 所以问题是,我在一个远程服务器上通过一个“for”循环检索一组图像。没问题,它能用。但我想给他们一个id,让他们在“数字”之后使用,这样我就可以在画布中使用它们,并为其设置动画。我不知道问题出在哪里,但是id的增加出错了。这是我的密码: <body> <button onclick="

第一篇文章是这样写的,但事实上是一个长期用户,因为我在这里找到了我需要的一切…直到现在

我在XHR方面遇到了一个棘手的问题。我可能会说我是JS的新手,但我没想到会发生这样的事情,我不能四处走动

所以问题是,我在一个远程服务器上通过一个“for”循环检索一组图像。没问题,它能用。但我想给他们一个id,让他们在“数字”之后使用,这样我就可以在画布中使用它们,并为其设置动画。我不知道问题出在哪里,但是id的增加出错了。这是我的密码:

<body>

<button onclick="loadCanvas(113);">Click here</button>
<canvas id="targetedCanvas" width="768" height="512"></canvas>
<script>
    window.URL = window.URL || window.webkitURL;        

    function loadImages() {
        for (id = 0; id < 114; id++) {
            var url = 'http://myurl.com/IMG_'+id+'.JPG';
            var request = new XMLHttpRequest();
            request.open('GET', url, true);
            request.responseType = 'blob';
            request.onload = function(e) {
                if(this.status == 200) {
                    var blob = this.response;

                    var img = document.createElement('img');
                    img.id = id;
                    img.onload = function(e) {
                        window.URL.revokeObjectURL(img.src);
                    };
                    img.src = window.URL.createObjectURL(blob);
                    img.width = 0;
                    img.height = 0;
                    document.body.appendChild(img);
                    }
                }
            request.send(null);
            };
    }

    function loadCanvas(id) {
        var canvas = document.getElementById('targetedCanvas');
        var context = canvas.getContext('2d');
        var img = document.getElementById(id);
        context.drawImage(img,0,0);
    };

    loadImages();

</script>
</body>

点击这里
window.URL=window.URL | | window.webkitURL;
函数loadImages(){
对于(id=0;id<114;id++){
var url='1〕http://myurl.com/IMG_“+id+”.JPG';
var request=new XMLHttpRequest();
打开('GET',url,true);
request.responseType='blob';
request.onload=函数(e){
如果(this.status==200){
var blob=this.response;
var img=document.createElement('img');
img.id=id;
img.onload=函数(e){
window.URL.revokeObjectURL(img.src);
};
img.src=window.URL.createObjectURL(blob);
img.width=0;
img.height=0;
文件.正文.附件(img);
}
}
请求发送(空);
};
}
函数loadCanvas(id){
var canvas=document.getElementById('targetedCanvas');
var context=canvas.getContext('2d');
var img=document.getElementById(id);
背景图像(img,0,0);
};
loadImages();
因此,您可以看到有一个按钮,当您单击它时,它会将图像加载到画布以显示它。 当我想显示id(
console.log(id);
)时,request.onload函数中的id是正常的(我的意思是,它类似于1、2、3…),但在函数中它总是113。这是我无法理解的事情。我想这是因为XHR是异步的,或者类似的…这就是我真正的新手

然后,如果有人有钥匙,或者知道什么东西可以用另一种方式使用XHR加载的图像,我很乐意阅读!)


非常感谢社区

您需要使用一个
自调用函数
,并在该函数中传递
id

因为请求本质上是异步的,所以直到您得到一个响应为止,for循环将完成,id将上升到114,所有分配的id将(可能)为114。所以用自调用函数保存它

 function loadImages() {
        for (id = 0; id < 114; id++) {
            (function(id){
            var url = 'http://myurl.com/IMG_'+id+'.JPG';
            var request = new XMLHttpRequest();
            request.open('GET', url, true);
            request.responseType = 'blob';
            request.onload = function(e) {
                if(this.status == 200) {
                    var blob = this.response;

                    var img = document.createElement('img');
                    img.id = id;
                    img.onload = function(e) {
                        window.URL.revokeObjectURL(img.src);
                    };
                    img.src = window.URL.createObjectURL(blob);
                    img.width = 0;
                    img.height = 0;
                    document.body.appendChild(img);
                    }
                }
            request.send(null);
            })(id);
            };
    }
函数loadImages(){
对于(id=0;id<114;id++){
(功能(id){
var url='1〕http://myurl.com/IMG_“+id+”.JPG';
var request=new XMLHttpRequest();
打开('GET',url,true);
request.responseType='blob';
request.onload=函数(e){
如果(this.status==200){
var blob=this.response;
var img=document.createElement('img');
img.id=id;
img.onload=函数(e){
window.URL.revokeObjectURL(img.src);
};
img.src=window.URL.createObjectURL(blob);
img.width=0;
img.height=0;
文件.正文.附件(img);
}
}
请求发送(空);
})(id);
};
}

这是一种过于复杂的方法,它依赖于定义本质上相同的回调方法的113个实例。为每个XHR对象分配一个id属性并在回调中读取它会更容易、更高效。非常感谢你们两位:-)我终于接受了@Touffy的解决方案。事实上,这并不难,但我没有想到这一点。