Javascript Parser error

感情迁移 提交于 2019-12-20 04:24:30

问题


I have a HTML5 video that has a poster and a CSS play overlay button. I am trying to get the video to load once it has ended so that it shows the poster and the play overlay button again.

I have tried the following code but get a Parser error on the last line, can anybody help me and let me know what it is I am doing wrong!?

$(document).ready(function() {
  $('.video').parent().click(function () {
    if ($(this).children(".video").get(0).paused) {
      $(this).children(".video").get(0).play();
      $(this).children(".playpause").fadeOut();
    } else {
      $(this).children(".video").get(0).pause();
      $(this).children(".playpause").fadeIn();
    }
  });

  var video= $(".video").get(0);   
  video.addEventListener('ended',function () {
    video.load(); 
    $(".playpause").show();                              
  }, false);
});

回答1:


To prevent

Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().

error, wrap if statement within click handler within setTimeout() call.

See also How to prevent "The play() request was interrupted by a call to pause()" error?

$(document).ready(function() {
  $('.video').parent().click(function () {
    var vid = $(this).children(".video").get(0);
    setTimeout(function() {
    if (vid.paused) {
      vid.play();
      $(this).children(".playpause").fadeOut();
    } else {
      vid.pause();
      $(this).children(".playpause").fadeIn();
    }
    })
  });

  var video= $(".video").get(0);   
  video.addEventListener('ended',function () {
    video.load(); 
    $(".playpause").show();                              
  }, false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
click<br>
<span class="playpause">play pause</span>
<video controls class="video" poster="https://placehold.it/350x150"  src="https://nickdesaulniers.github.io/netfix/demo/frag_bunny.mp4"></video>
</div>


来源:https://stackoverflow.com/questions/41108925/javascript-parser-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!