Question

Why is the 1st block of code working while the 2nd one is not?? --

The following code works:-

// this code works --

<script src=".../popcorn-complete.min.js"></script>

    var time_prompts = new Array();
    time_prompts[0] = 2  //any integer < video duration

      $popcorn.on( "timeupdate", function() {
          console.log( this.currentTime() );
          if (this.currentTime() > time_prompts[0] && this.currentTime() < time_prompts[0]+0.1) {
              this.pause();
              console.log(this.paused)
          }  
      });

While the following code doesn't work:-

// this code DOES NOT work

    <script src=".../popcorn-complete.min.js"></script>

        var time_prompts = new Array();
        time_prompts[0] = 2  //any integer < video duration

          $popcorn.on( "timeupdate", function() {
              console.log( this.currentTime() );
              if (this.currentTime() == time_prompts[0]) {
                  this.pause();
                  console.log(this.paused)
              }  
          });

( The only difference between the 2 code blocks is the 'if statement' (condition) )

Was it helpful?

Solution

This happens because this.currentTime() is not an integer. It is a Number containing a floating point value. As you can see in the jsfiddle demo, provided in the documentation, it is unlikely to get a round integer value. So checking for equality is inappropriate in this case. You did not specify what did you want to achieve with your code, but if you want to pause the video after it reaches 2 second in play, you should check if currentTime is greater than 2, and then set a variable that will ensure you do this only once:

var time_prompts = [], once = true;
time_prompts[0] = 2  //any integer < video duration

  $popcorn.on( "timeupdate", function() {
      console.log( this.currentTime() );
      if (this.currentTime() > time_prompts[0] && once) {
          once = false;
          this.pause();
          console.log(this.paused)
      }  
  });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top