首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解Popcorn.js时间更新功能有困难,因为它在某些情况下不起作用

理解Popcorn.js时间更新功能有困难,因为它在某些情况下不起作用
EN

Stack Overflow用户
提问于 2013-08-20 04:16:35
回答 1查看 522关注 0票数 0

为什么第一个代码块工作,而第二个代码不是

下列代码起作用:

代码语言:javascript
复制
// 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)
          }  
      });

而以下代码不起作用:

代码语言:javascript
复制
// 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)
              }  
          });

( 2代码块之间唯一的区别是'if语句‘(条件))

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-20 08:12:59

之所以会发生这种情况,是因为this.currentTime()不是整数。它是一个包含浮点值的Number。正如您在小提琴演示中看到的,在文档中提供的,它不太可能得到一个圆形整数值。因此,在这种情况下,检查是否平等是不合适的。您没有指定您想用代码实现什么,但是如果您想在视频达到2秒后暂停播放,您应该检查currentTime是否大于2,然后设置一个变量,以确保只执行一次:

代码语言:javascript
复制
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)
      }  
  });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18326882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档