Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript html5音频:更改currentTime会触发多个canplay事件_Javascript_Jquery_Html5 Audio - Fatal编程技术网

Javascript html5音频:更改currentTime会触发多个canplay事件

Javascript html5音频:更改currentTime会触发多个canplay事件,javascript,jquery,html5-audio,Javascript,Jquery,Html5 Audio,我需要更改HTML5音频元素的偏移时间。我已经编写了一个函数来处理canplay事件并更改currentTime属性 但是,更改currentTime属性似乎会触发事件,因此它会在循环中继续。当我注释掉currentTime Changed时,它正常工作(即事件只触发一次) 正确的方法是什么?这种行为至少发生在Firefox和Chrome中。因此,音乐无法正常播放 <audio id="current" src="http://www.music.helsinki.fi/tmt/opetu

我需要更改HTML5音频元素的偏移时间。我已经编写了一个函数来处理canplay事件并更改currentTime属性

但是,更改currentTime属性似乎会触发事件,因此它会在循环中继续。当我注释掉currentTime Changed时,它正常工作(即事件只触发一次)

正确的方法是什么?这种行为至少发生在Firefox和Chrome中。因此,音乐无法正常播放

<audio id="current" src="http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav" controls 
start="1000" end="2000">    
</audio>
<script>

 $('audio').bind('canplay', function(event) {
     alert("fire!");
    event.target.currentTime = 0.5;
 });
 </script>


实际上,它似乎适用于加载元数据事件,但每个人都建议canplay更好,并且与更多浏览器兼容

您可以使用.one而不是.bind,以便只在第一次收听它

$('audio').one('canplay', function(event) {
    console.log("fire!");
    event.target.currentTime = 0.5;
});

您可以将
.bind
更改为
.one
——这样它只会触发一次。@naortor非常感谢您!现在可以用了