简体   繁体   English

javascript for循环重复上一次迭代

[英]javascript for-loop repeating last iteration

I am trying to crop-resize n display an image on client-browser using JS. 我正在尝试裁剪大小n使用JS在客户端浏览器上显示图像。 I am able to do so except that my text loop is wrong. 我可以这样做,但我的文本循环是错误的。 You can see in the image below that it is repeating the last iteration. 您可以在下面的图像中看到它正在重复上一次迭代。 My image loop works fine though. 我的图像循环工作正常。 Hint - First filename text is supposed to be black.jpg . Hint -第一个文件名文本应该是black.jpg

在此处输入图片说明

Am unable to solve the issue after having tried for several hours. 尝试了几个小时后无法解决问题。 Given below is a trimmed version of the code. 下面给出的是代码的精简版本。 If needed here is the complete version of the script. 如果需要,这里是脚本的完整版本 Please help, I am still learning. 请帮助,我还在学习。

HTML 的HTML

<input type="file" id="input" onchange="prevCrop()" multiple> 
<ul id="listWrapper"></ul> 

JAVASCRIPT JAVASCRIPT

function prevCrop() {
    var files = document.querySelector('input[type=file]').files;

    for (var i = 0; i < files.length; i++) {
        var file = files[i]; 
        var fileSize = Math.floor(file.size/1024);
        var info = file.name + " " + fileSize + " KB : iteration number - " + i;

        var reader = new FileReader();
        if (reader != null) {
        reader.onload = GetThumbnail;
        reader.readAsDataURL(file);
        }           
    }


    function GetThumbnail(e) {
        var canvas = document.createElement("canvas");
        var newImage = new Image();
        newImage.src = e.target.result;
        newImage.onload = cropResize; 

        function cropResize() {
            canvas.width = 70;
            canvas.height = 70;
            ### more codes here that handles image   ###

            var dataURL = canvas.toDataURL("image/jpg");
            var thumbList = document.createElement('div');
            thumbList.setAttribute('class', 'tlistClass');  
            var nImg = document.createElement('img'); 
            nImg.src = dataURL;     
            var infoSpan = document.createElement('span');                      
            infoSpan.innerHTML = info;          
            var handle = document.getElementById("listWrapper").appendChild(thumbList);
            handle.appendChild(nImg);
            handle.appendChild(infoSpan);   
        }               
    }

}

This happens because the onload callback function is triggered asynchronously, so after your code has already ended. 发生这种情况是因为onload回调函数是异步触发的,因此代码已结束。 At that time info has the string that was assigned to it in the last iteration. 那时info具有在上一次迭代中分配给它的字符串。 Only then the onload events are fired, resulting in the different calls of GetThumbnail , which all will see the same value for info . 只有这样,才会触发onload事件,从而导致对GetThumbnail的不同调用,所有调用都将看到相同的info值。

Instead bind the value of info to the callback function like this: 而是将info的值绑定到回调函数,如下所示:

    reader.onload = GetThumbnail.bind(null, info);

...and define the corresponding parameter for it in the GetThumbnail function: ...并在GetThumbnail函数中为其定义相应的参数:

function GetThumbnail(info, e) {

That way every call of that function will posses of the proper value for info . 这样,该函数的每次调用都将为info赋予适当的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM