数组无法填充

发布于 2024-11-02 01:19:23 字数 1100 浏览 0 评论 0原文

function draw(data)
{
var i = 0;
var checkduplicates = new Array();
drawOne(i);
//console.log(checkduplicates) Problem!!!;

function drawOne(i)
{
         //photos is a object that has been omitted for simplicity
    var picinfo = photos[Math.floor(Math.random()*photos.length)];
    checkduplicates.push(picinfo);

    img.onload = function()
    {
        var ctx = document.getElementsByClassName("canvas")[i].getContext('2d');
        // Draw pieces
        ctx.drawImage(img,0,0,132,150);
        //ctx.fillText("haha",0,0);
        ctx.drawImage(frame,0,0,133,152);
        i++;
        if (i != canvaslength)
        {
             drawOne(i);
        }
    }
    //console.log(checkduplicates.length);
}
}

嘿,我在draw函数内部声明了drawOne函数,这样内部函数drawOne就可以访问局部变量checkduplicates。然而,在drawOne函数运行16次之后,数组checkduplicates应该填充了对象,这由drawOne内部的console.log证明,显示数组中有16个对象。然而,我用“问题”标记的 console.log 函数显示该数组只有第一个对象,而其他 console.logs 显示该数组充满了对象。我不明白为什么,因为修改数组的唯一方法是通过“push”方法,该方法已被调用 16 次,如果我注释掉 img.onload 部分,带有注释“problem”的 console.log 将显示该数组充满了物体。为了简单起见,我故意省略了其他代码,并且我的萤火虫不会抱怨错误。谢谢

function draw(data)
{
var i = 0;
var checkduplicates = new Array();
drawOne(i);
//console.log(checkduplicates) Problem!!!;

function drawOne(i)
{
         //photos is a object that has been omitted for simplicity
    var picinfo = photos[Math.floor(Math.random()*photos.length)];
    checkduplicates.push(picinfo);

    img.onload = function()
    {
        var ctx = document.getElementsByClassName("canvas")[i].getContext('2d');
        // Draw pieces
        ctx.drawImage(img,0,0,132,150);
        //ctx.fillText("haha",0,0);
        ctx.drawImage(frame,0,0,133,152);
        i++;
        if (i != canvaslength)
        {
             drawOne(i);
        }
    }
    //console.log(checkduplicates.length);
}
}

Hey, I declared the drawOne function inside the draw function, so that the local variable checkduplicates is accessible to the inner function drawOne. However, after the drawOne function runs 16 times, the array checkduplicates is supposed to be populated with objects, which is proved by the console.log inside drawOne that shows 16 objects are in the array. However, the console.log function, which I marked with "Problem", shows that the array has only very first object meanwhile others console.logs show the array is full of objects. I dont understand why since the only way to modify the array is through the "push" method, which has been called for 16 times and if I comment out the img.onload part, the console.log with comment "problem" shows the array is full of objects. I intentionally omitted other codes for simplicity and my firebug doesnt complain for bugs. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-11-09 01:19:23

当您使用异步递归时,对 drawOne 的初始调用将在第一次迭代后返回。当第一个图像加载后,以及当您退出 draw 函数以便浏览器重新获得控制权以便能够处理事件时,迭代将继续。

因此,数组在第一次迭代后仅包含单个项目根本不是问题,这是您使用的迭代方法的预期结果。

As you are using asynchronous recursion, the initial call to drawOne will return after the first iteration. The iterations will continue when the first image has loaded, and when you have exited out of the draw function so that the browser regains control so that it can handle events.

So, that the array only contains a single item after the first iteration is not a problem at all, it's the expected result from the iteration method that you have used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文