Javascript变量恢复原状

时间:2013-09-27 05:05:03

标签: javascript

function loadAll() {
    var zip64;
    var zipURL = 'settings/Images.zip';
    var xhr = new XMLHttpRequest();
    xhr.open('GET', zipURL, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() {
        if (this.status == 200) {
            var responseArray = new Uint8Array(this.response);
            var d = responseArray.length;
            var binaryString = new Array(d);

            while (d--) {
                binaryString[d] = String.fromCharCode(responseArray[d]);
            }

            var data = binaryString.join("");
            zip64 = window.btoa(data);

            zip.createReader(new zip.Data64URIReader(zip64), function(reader) {
                reader.getEntries(function(entries) {
                    z = 0;
                    zloopid = setInterval(function() {
                        if (z >= entries.length || !entries[z]) {
                            clearInterval(zloopid);
                            reader.close();
                            start();
                            return;
                        }

                        if (entries[z].filename.split("/")[0] == "__MACOSX") {
                            z++;
                            return;
                        }

                        $("#loadimg").html("Loading " + entries[z].filename + ".... (" + (z + 1) + " of " + entries.length + ")");

                        var isMask = (entries[z].filename.replace(/^.*[\\\/]/, '') == "mask.jpg");

                        entries[z].getData(new zip.Data64URIWriter(), function(uri) {
                            if (isMask) {
                                imgtemp = new Image();
                                imgtemp.src = uri;
                            }
                            else {
                                var lol = new Image();
                                lol.src = uri;
                                imageLibrary.push(lol);
                            }
                        });

                        z++;
                    }, 0);
                });
            }, function(error) {
                window.location = "error.html?err=Error by unzipper: " + error + " (Problem in Images.zip)";
            });
        }
        else {
            window.location = "error.html?err=Error while retrieving zip file (" + zipURL + ") : " + this.status;
        }
    };
    xhr.send();
}

function start() {
    var image = imgtemp;
    width = image.width;
    height = image.height;

所以我有这段代码。我有一个名为imgtemp的全局变量,但每当我在if (inMask)块中设置它时,它会在调用start()时恢复为未定义。这是为什么?

首先调用loadAll(),然后调用start()。我可以确认,当执行loadAll()中调用start()的代码时,imgtemp已经转回到undefined。

提前致谢!

1 个答案:

答案 0 :(得分:0)

这是因为loadAll变为异步并在加载任何内容之前返回,因此当您调用start时,尚未设置图像。你需要“链接”它们而不是单独调用它们。

只调用loadAll(),然后在loadAll中添加一个事件处理程序,用于加载图像,加载时调用start()函数。

/* snip */
entries[z].getData(new zip.Data64URIWriter(), function(uri) {
    if (isMask) {
        imgtemp = new Image();
        imgtemp.onload = function(event) {
            start(); /* <--- HERE  */
        }
        imgtemp.src = uri;
    }
    else {
/* snip */