当 javascript while 循环运行时图像不会加载

发布于 2024-09-11 21:30:09 字数 732 浏览 3 评论 0原文

我正在为我的 html5 项目编写一个图像预加载器,但遇到了一个问题。游戏中使用的图像是用 JavaScript 中的图像对象定义的,如下所示:

images = new Array();
images[0] = new Image();
images[0].src = '/images/tiles/grass.png';

一旦“document.ready”,预加载器函数就会运行,如下所示。

function preLoader(){
    while(loaded != images.length){
        var loaded = 0;
        for(var loadTest = 0; loadTest < images.length; loadTest++){
            if(images[loadTest].complete){
                loaded ++;
            }
        }
        console.log(loaded);
    }
    console.log("loaded all");
}

问题是循环永远不会结束,因为 images[loadTest].complete 总是返回 false。我已经测试过在页面加载后几秒钟图像已加载完毕后运行该函数并且它工作得很好。

因此,我假设 while 循环运行正在阻止图像加载。如果这是真的,我将如何解决这个问题?

I'm writing an image pre-loader for my html5 project and I've struck a problem. Images that are used in the game are defined with the image object within JavaScript like so:

images = new Array();
images[0] = new Image();
images[0].src = '/images/tiles/grass.png';

As soon as "document.ready" the pre-loader function as seen below is run.

function preLoader(){
    while(loaded != images.length){
        var loaded = 0;
        for(var loadTest = 0; loadTest < images.length; loadTest++){
            if(images[loadTest].complete){
                loaded ++;
            }
        }
        console.log(loaded);
    }
    console.log("loaded all");
}

The problem is that the loop never ends because images[loadTest].complete always returns false. I've tested running the function a few seconds after the page loads when the image has defiantly loaded and it works just fine.

Due to this I'm assuming that the while loop running is stopping the images from loading. If that is true how would I go about fixing this problem?

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

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

发布评论

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

评论(4

佞臣 2024-09-18 21:30:09

除了 onload 事件之外,您还可以通过 setTimeout 或 setInterval 调用加载图像或进行轮询。这会导致代码以非阻塞方式运行。

setInterval('checkimages()', 1000)

Alternatively to an onload event you can load images or do polling via a setTimeout or setInterval call. This causes the code to run in a non-blocking fashion.

setInterval('checkimages()', 1000)
路还长,别太狂 2024-09-18 21:30:09

传递给 .ready() 的处理程序保证在之后执行DOM 已构建,但在接收图像等资产之前。

您可能想使用 .load() 处理程序:

$(window).load(function () {
  // run code when the page is fully loaded including graphics.
});

这是假设您正在使用 jQuery。

The handler passed to .ready() is guaranteed to be executed after the DOM is constructed, but before assets such as images are received.

You may want to use the .load() handler instead:

$(window).load(function () {
  // run code when the page is fully loaded including graphics.
});

This is assuming you are using jQuery.

梦醒时光 2024-09-18 21:30:09

您的代码中有一些错误。

  • 最大的问题是你正在循环中进行轮询,这在 Javascript 中是不应该做的。 Javascript 与页面在同一线程中运行,因此 Javascript 中的处理会阻止页面中的其他操作,包括加载和用户交互。一般来说,在没有某种延迟的情况下进行轮询也是一个坏主意,在此期间您放弃了对 CPU 的控制。

    轮询应该使用 setInterval() 或 setTimeout() 而不是循环来完成。

  • 另一个问题是在循环期间,每次循环运行时加载一个或多个图像时,loaded 变量都会递增,因此 loaded 可能会达到images.length 不是当所有图像都已加载时,而是当至少一个图像已加载时。

You have some errors in your code.

  • The big problem is that you are doing polling in a loop, which you shouldn't do in Javascript. Javascript is run in the same thread as the page, so processing in Javascript blocks other things in the page including loading and user interaction. It's also a bad idea in general to do polling without some kind of delay during which you relinquish control of the CPU.

    Polling should be done using setInterval() or setTimeout() rather than a loop.

  • Another problem is during your loop, your loaded variable is incremented any time one or more images has loaded each time the loop runs, so that loaded is likely to reach images.length not when all images have loaded but when at least one has loaded.

剩一世无双 2024-09-18 21:30:09

您可以使用 Image.onload 事件。我不太擅长 Javascript,但类似这样的东西应该可以工作并避免使用计时器:

var loadCounter = 0;

function imageLoaded() {
    loadCounter++;
    if(loadCounter == images.length) {
        console.log("images loaded");
    }
}

images = new Array();
images[0] = new Image();
images[0].onload = imageLoaded;
images[0].src = '/images/tiles/grass.png';

基本上,每当加载图像时,我们都会增加计数器。如果所有图像均已加载,我们将记录输出。

You can use the Image.onload event. I'm not very good with Javascript, but something like this should work and avoids using timers:

var loadCounter = 0;

function imageLoaded() {
    loadCounter++;
    if(loadCounter == images.length) {
        console.log("images loaded");
    }
}

images = new Array();
images[0] = new Image();
images[0].onload = imageLoaded;
images[0].src = '/images/tiles/grass.png';

Basically, whenever an image is loaded, we increment the counter. If all images have been loaded, we log the output.

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