为什么第一个代码块工作,而第二个代码不是?
下列代码起作用:
// 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)
}
});而以下代码不起作用:
// 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语句‘(条件))
发布于 2013-08-20 08:12:59
之所以会发生这种情况,是因为this.currentTime()不是整数。它是一个包含浮点值的Number。正如您在小提琴演示中看到的,在文档中提供的,它不太可能得到一个圆形整数值。因此,在这种情况下,检查是否平等是不合适的。您没有指定您想用代码实现什么,但是如果您想在视频达到2秒后暂停播放,您应该检查currentTime是否大于2,然后设置一个变量,以确保只执行一次:
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)
}
});https://stackoverflow.com/questions/18326882
复制相似问题