正如标题所述,当设置currentTime
以编程方式指定音频元素的属性。设置该值后,事件侦听器将无限期地反复触发。
音频将尝试播放,但由于正在进行太多处理,音频将跳过,我的浏览器将开始变得无响应。
我通过删除对的绑定解决了这个问题currentTime
变量,但我只是想问是否有人对为什么会发生这种情况有所了解?最初,这种情况只发生在 Firefox 上,但今天也开始发生在 Chrome 上。在 Safari 或 IE 11 中不会发生。
这是一个例子:https://jsbin.com/yorawo/edit?html,控制台,输出 https://jsbin.com/yorawo/edit?html,console,output
想法?
<body>
<button onclick="play()">Play</button>
<button onclick="pause()">Pause</button>
<audio id="audio" src="https://api.soundcloud.com/tracks/172463126/download?client_id=02gUJC0hH2ct1EGOcYXQIzRFU91c72Ea&oauth_token=1-138878-53859839-4dcd0ce624b390"></audio>
<script>
var audio = document.getElementById('audio');
// remove the line below (audio.currentTime = 0) and then try to play the audio. notice the audio plays just fine.
// then add the line back in and notice how the console will fill up
audio.oncanplay = function () {
audio.currentTime = 0;
}
audio.ontimeupdate = function () {
console.log('why');
}
function play() {
audio.play();
}
function pause() {
audio.pause();
}
</script>
</body>
我将复制@dandavis 提到的内容:
当你跳到时间上时,在你可以玩之前有一段时间,所以 canplay() 在你可以再次玩后触发
在上面的代码块中,我将 currentTime 设置为某个值,并且我已设置oncanplay
事件处理程序重置该值。每当currentTime
被设置为一个值,oncanplay
被解雇了。由于我正在重置currentTime
in the oncanplay
处理程序,此事件会无休止地发出。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)