1

I discovered a bug in the code editor. The following code has two videos, which should automatically play if the video is in the viewport and pause if the video is not in the viewport anymore.

This works if you edit the question, press on "edit the above snippet" and then run the code from there. But it does not work if you press on the button "Run code snippet" in the question.

// Limitation: Does not work if the element is
// out of view because it is too far right or left
$.fn.isInViewport = function() {
    var elementTop = $(this).offset().top;
    var elementBottom = elementTop + $(this).outerHeight();

    var viewportTop = $(window).scrollTop();
    var viewportBottom = viewportTop + $(window).height();

    return elementBottom > viewportTop && elementTop < viewportBottom;
};

setInterval(function() {
    $('video').each(function(){
        if ($(this).isInViewport()) {
            $(this)[0].play();
        } else {
            $(this)[0].pause();
        }
    });
}, 1000);
#right {
  position: absolute;
  top: 2000px;
}
#video1 {
  position: absolute;
  left: 0px;
  top: 1000px;
}
#video2 {
  position: absolute;
  left: 0px;
  top: 2000px;
}

body {
  width: 500px;
  height: 3000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="info"></div>

<div id="down">
  scroll down please...
</div>

<video id="video1" controls>
  <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4">
  <source src="https://www.w3schools.com/html/movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>

<video id="video2" controls>
  <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4"/>
  <source src="https://www.w3schools.com/html/mov_bbb.ogg" type="video/ogg"/>
  Your browser does not support the video tag.
</video>

11
  • 3
    What's your browser and do you have any browser add-ons? This is working fine for me on Chrome for MacOS and Android. Commented Dec 9, 2020 at 13:22
  • I am using Chrome 86.0.4240.198 (Offizieller Build) (64-Bit) with many extensions, windows 10. Commented Dec 9, 2020 at 13:32
  • I tested it again in inspector mode with no extensions and it still does not work. Commented Dec 9, 2020 at 13:35
  • 1
    works fine for me using FF on windows Commented Dec 9, 2020 at 13:47
  • 3
    I guess this is a status-norepro... It's working fine for me. Commented Dec 9, 2020 at 13:48
  • 1
    Edge on Windows 10 - works fine for me. Both videos play. You admit you have "many extensions". So try removing all of them and see if it works for you. Commented Dec 9, 2020 at 14:00
  • 1
    I'm on Chrome Latest/ Win 10 and works fine for. Both play and stop according to your description and expectation. Also checked when on full page. Commented Dec 9, 2020 at 14:07
  • @rene "bug" is that it's not auto playing. Commented Dec 9, 2020 at 14:09
  • @Shadow10YearsWizard it is auto-playing for me? Commented Dec 9, 2020 at 14:10
  • "You admit you have "many extensions". So try removing all of them and see if it works for you" I already wrote that I did that but it still fails. Commented Dec 9, 2020 at 14:14
  • @rene very weird, for me it's not auto playing, same OS/browser. Commented Dec 9, 2020 at 14:37

1 Answer 1

6

It's not a bug on Stack Exchange's side.

If you open up the dev console, you'll see a clear error message:

Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

A possible fix can be found on Stack Overflow: How to handle "Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first." on Desktop with Chrome 66?.

3
  • 2
    You are correct! If I add muted="muted" to the video tags, then it works! Commented Dec 9, 2020 at 14:18
  • muted alone is enough btw. You dont have to use muted="muted" Commented Dec 10, 2020 at 10:10
  • 1
    @Black cheers, consider adding this as comment to the SO answer as well. Commented Dec 10, 2020 at 12:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.