Javascript 使用背景视频刷新页面

Javascript 使用背景视频刷新页面,javascript,ajax,html,html5-video,video-player,Javascript,Ajax,Html,Html5 Video,Video Player,在我的网站上,后台播放视频。当我转到另一页时,如何在同一点上恢复视频?使用ajax刷新页面。 我试图通过主页上的小脚本来解决这个问题: <script> var vid = document.getElementById("video"); vid.addEventListener("canplay", function(e) { var currTime = this.currentTime; this.play(); }, false ); </script>

在我的网站上,后台播放视频。当我转到另一页时,如何在同一点上恢复视频?使用ajax刷新页面。 我试图通过主页上的小脚本来解决这个问题:

<script>
var vid = document.getElementById("video");
vid.addEventListener("canplay", function(e) {
  var currTime = this.currentTime;
  this.play();
 }, false
);
</script>
在页面上,我得到的暂停视频代码相同,但id不同

<script>
var vid = document.getElementById("video");
vid.addEventListener("canplay", function(e) {

localStorage.setItem("videoTime", this.currentTime);
  this.play();
 }, false
);
</script> 
重新加载后,使用以下内容获取项目:

        <script>
    var vid = document.getElementById("myVideo");
        vid.addEventListener("canplay", function(e) {
        if(localStorage.getItem("videoTime") !== null) {
           this.currentTime = localStorage.getItem("videoTime");
        }

        this.play();
     }, false
     );
    </script>

var vid=document.getElementById(“myVideo”);
参见addEventListener(“canplay”,函数(e){
if(localStorage.getItem(“videoTime”)!==null){
this.currentTime=localStorage.getItem(“videoTime”);
}
这个。play();
},错
);
更新:

在我的机器上测试过。适用于任何.html文件。只需将其粘贴为脚本:

<script>
        window.onload = () => {
            var vid = document.getElementById("video");
            if(localStorage.getItem("videoTime") !== null && localStorage.getItem("videoTime") !== undefined) {
                vid.currentTime = localStorage.getItem("videoTime");   }                  

window.onload=()=>{
var vid=document.getElementById(“视频”);
if(localStorage.getItem(“videoTime”)!==null&&localStorage.getItem(“videoTime”)!==未定义){
vid.currentTime=localStorage.getItem(“videoTime”);}
setInterval(()=>{localStorage.setItem(“videoTime”,vid.currentTime);},1000);}

        </script> 

  • 在加载窗口后执行脚本
  • 获取视频作为HTMLElement
  • 检查键为
    vidTime
    的localStorage条目是否存在
  • 如果是,请使用
    vid.currentTime=localStorage.getItem(“videoTime”)设置vid time
  • 每秒更新一次新的videoTime:
    setInterval(()=>{localStorage.setItem(“videoTime”,video.currentTime);},1000)

  • 您可能希望连续读取视频上的当前时间戳,并将其保存到localStorage。然后,当页面重新加载时,从localStorage检查该时间戳并将视频加载到该时间。@daddygames是的,听起来就像我需要的那样。我怎么能做到?通过创建js文件并读取时间戳并将其写入这些文件?效果很好,但在刷新视频暂停后,我尝试了
    this.playing()但仍然暂停。
    $('myVideo')。触发器('play')不帮助
    $('myVideo')。获取(0.play()
    help@AlexanderVasilchuk添加了一个更新。
    setInterval(() => {localStorage.setItem("videoTime", this.currentTime);},1000);
    
            <script>
        var vid = document.getElementById("myVideo");
            vid.addEventListener("canplay", function(e) {
            if(localStorage.getItem("videoTime") !== null) {
               this.currentTime = localStorage.getItem("videoTime");
            }
    
            this.play();
         }, false
         );
        </script>
    
    <script>
            window.onload = () => {
                var vid = document.getElementById("video");
                if(localStorage.getItem("videoTime") !== null && localStorage.getItem("videoTime") !== undefined) {
                    vid.currentTime = localStorage.getItem("videoTime");   }                  
    
            </script>