Javascript InvalidStateError: 尝试使用不可用或不再可用的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34970272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:07:59  来源:igfitidea点击:

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

javascriptfirefoxhtml5-video

提问by Phillip Senn

The following works in Chrome but not Firefox:

以下内容适用于 Chrome,但不适用于 Firefox:

var myVideo = document.getElementById('myVideo')
myVideo.currentTime = 570
<video id="myVideo" controls>
<source src="myVideo.mp4" type="video/mp4">
</video>

In Firefox it says

在 Firefox 中它说

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

InvalidStateError: 尝试使用不可用或不再可用的对象

for line 2.

对于第 2 行。

回答by adeneo

That error occurs when the object, in this case the video, hasn't loaded enough to be able to set the currentTimeand skip forward.

当对象(在本例中为视频)尚未加载到足以设置currentTime和 向前跳过时,就会发生该错误。

You'd have to wait until the video can be played before you can set the currentTime

您必须等到视频可以播放后才能设置 currentTime

var myVideo = document.getElementById('myVideo')

myVideo.addEventListener('canplaythrough', function() {
    myVideo.currentTime = 570;
}, false);