【问题标题】:Why console.log shows me that there are 0 loaded images in the html document?为什么 console.log 显示 html 文档中有 0 个加载的图像?
【发布时间】:2021-12-15 06:37:01
【问题描述】:

我一直在根据一个 youtube 教程根据 html 文档中的图像计数来制作预加载器。

我卡住了,不明白为什么 console.log 命令说加载的图像数量为 0,尽管我在 html 文档中有一些图像。

我已经制作了代码框,以便人们可以轻松查看代码: https://codesandbox.io/s/preloader-mip2o?file=/app.js

感谢您的帮助!

【问题讨论】:

    标签: javascript preloader


    【解决方案1】:

    它发生了,因为 JavaScript 是异步工作的。 我在image_loaded 函数中添加了 1 行:

    function image_loaded() {
      alert("In image_loaded")
      images_loaded_count++;
    ...
    

    之后我在浏览器中运行它,发现在实际加载图像之前调用了 console.log。

    【讨论】:

    • 谢谢!在我看来,关于 console.log 是一个愚蠢的错误 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2014-12-17
    • 2019-10-28
    相关资源
    最近更新 更多