结束 onmousedown 事件(onmouseup?)

Ending an onmousedown event (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()"/>

尝试使用 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()"/>