gpt4 book ai didi

JavaScript 图像同步

转载 作者:行者123 更新时间:2023-12-01 01:50:17 33 4
gpt4 key购买 nike

如果在运行脚本之前图像已加载到 DOM 中(使用标签),则以下任一注释是否正确?

var image = new Image();
image.onload = function(){}
image.src = "already-loaded-earlier.png";

// image.onload is guaranteed to be called right now
// image is guaranteed to be loaded and available for use right now

最佳答案

Image onload 回调始终异步调用。如果图像已加载,则在下一个tick时调用onload。下面的代码片段在 end 行之后输出 image is returned secondary time 行。

var img = new Image();
img.onload = function() {
console.log('image is loaded first time');
var img = new Image();
img.onload = function() {
console.log('image is loaded second time');
};
img.src = '/image/Bt4Tq.jpg?s=32&g=1';
console.log('end');

// you can use img, without waiting `onload` to be called
console.log(img.width, img.height);
};
img.src = '/image/Bt4Tq.jpg?s=32&g=1';

关于JavaScript 图像同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51612447/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com