小编Tom*_*Tom的帖子

如果在没有将其附加到页面的情况下播放,使用 document.createElement 创建的视频元素何时会被垃圾收集?

假设我创建了一个视频并像这样播放:

async function createVideo() {
    const videoEl = document.createElement("video");
    videoEl.autoplay = true;
    videoEl.src = "https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4";

    await videoEl.play()
    return videoEl
}

createVideo()
    .then(() => console.log('video is playing'))
    .catch(error => console.log(error))
Run Code Online (Sandbox Code Playgroud)

在 chrome 上,这会导致视频播放,除非元素实际存在于 DOM 中。

该视频将在什么时候被垃圾收集?

此外,如果在返回视频元素之前createVideo()运行video.pause()在内部,垃圾收集将如何工作?视频元素会永远像僵尸一样存在吗?

javascript dom garbage-collection html5-video

5
推荐指数
1
解决办法
213
查看次数

标签 统计

dom ×1

garbage-collection ×1

html5-video ×1

javascript ×1