结束onmousedown事件(onmouseup?)

时间:2017-05-31 10:59:17

标签: javascript loops audio onmousedown onmouseup

我使用onmousedown事件创建了一个按钮,该事件在单击时循环播放音轨。我正在尝试在单击相同按钮时结束音频循环。我尝试了onmouseup活动,但没有运气,任何想法? (我是JavaScript的新手)

<script> var one = new Audio(); one.src = "files/audio/one.wav"; one.loop = true; </script>

<img class="item" src="files/button.png" onmousedown="one.play()" onmouseup="one.stop()"/>

1 个答案:

答案 0 :(得分:1)

尝试使用var跟踪您的点击次数,只收听click事件:

<强> JS

var one = new Audio(); 
one.src = "files/audio/one.wav"; 
one.loop = true;
var isPlaying = false;

function manage(){
  if(isPlaying){
    one.pause();
    one.currentTime = 0;
    isPlaying = false;
  }
  else{
    one.play();
    isPlaying = true;
  }
}

<强> HTML

<img class="item" src="files/button.png" onclick="manage()"/>