英文:
Video Player Local Storage
问题
我想要将视频的duration
和src
存储到本地存储中,但这段代码不起作用。视频的src
和duration
在检查元素应用程序本地存储中显示,但对于视频播放器不起作用。
window.addEventListener("unload", () => {
let setDuration = localStorage.setItem("duration", `${mainVideo.currentTime}`);
let setSrc = localStorage.setItem("src", `${mainVideo.getAttribute("src")}`);
});
window.addEventListener("load", () => {
let getDuration = localStorage.getItem("duration");
let getSrc = localStorage.getItem("src");
if (getSrc) {
mainVideo.src = getSrc;
mainVideo.currentTime = getDuration;
}
});
请注意,这段代码应该放在适当的上下文中,确保mainVideo
已正确定义。
英文:
I want to store video duration
and ``src to local storage, but this code not working. The video src
and duration
showing in inspect element application local storage but not working for the video player.
window.addEventListener("unload", () => {
let setDuration = localStorage.setItem(
"duration",
`${mainVideo.currentTime}`
);
let setSrc = localStorage.setItem("src", `${mainVideo.getAttribute("src")}`);
});
window.addEventListener("load", () => {
let getDuration = localStorage.getItem("duration");
let getSrc = localStorage.getItem("src").src;
if (getSrc) {
mainVideo.src = getSrc;
mainVideo.currentTime = getDuration;
}
});
答案1
得分: 2
一个问题肯定就在这里
localStorage.getItem("src").src
您试图读取属性src
,但localStorage.getItem("src")
返回的是一个字符串
或null
而不是一个对象
。删除.src
部分,该错误将得到解决。
如果这不是您的问题,我很抱歉。如果是的话,请提供有关您的问题或任何错误消息的更多信息。
英文:
One problem certainly lays right here
localStorage.getItem("src").src
You are trying to read the property src
but localStorage.getItem("src")
returns a string
or null
not an object
. Remove the .src
part and that error will be resolved.
I'm sorry if that wasn't your problem. If so please provide further information about your problem or any error messages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论