javascript - trouble understanding Popcorn.js timeupdate function as it is not working in certain cases -


why 1st block of code working while 2nd 1 not?? --

the following code works:-

// 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 following code doesn't work:-

// code 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 difference between 2 code blocks is 'if statement' (condition) )

this happens because this.currenttime() not integer. number containing floating point value. can see in jsfiddle demo, provided in documentation, unlikely round integer value. checking equality inappropriate in case. did not specify did want achieve code, if want pause video after reaches 2 second in play, should check if currenttime greater 2, , set variable ensure 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)       }     }); 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -