最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS HTMLImageElement and garbage collection - Stack Overflow

programmeradmin1浏览0评论

Am I required to hold ref to HTMLImageElement to prevent it from being gc'ed befor its load/error events will be fired?

for example:

/**
 * @param { string[] } urls 
 * @returns { Promise<HTMLImageElement[]> }
 */
function loadImages(urls) {
    return new Promise(function(resolve) {
        /**@type { HTMLImageElement[] } */
        const images = [];
        let i = urls.length;
        /**@this { HTMLImageElement } */
        function onLoad() {
            images.push(this);
            if (--i === 0) resolve(images);
        }
        function onError() {
            if (--i === 0) resolve(images);
        }
        for (const url of urls) {
            const image = new Image();
            image.src = url;
            image.addEventListener("load", onLoad);
            image.addEventListener("error", onError);
        }
    });
}

is it possible to some of images being lost due to gc or not ?

it looks like i really need to do it, but there also can be some special rule for cases like this

发布评论

评论列表(0)

  1. 暂无评论