按住时如何使按钮只播放一次声音?

时间:2018-08-23 13:02:45

标签: javascript dom

我已经构建了一个简单的Drumpkit应用https://napvlm.github.io/projects/drumpad-app/

如果我按住键盘上的琴键,声音就会反复播放,这会导致声音破裂。按住时如何使按钮只播放一次声音?

这是代码:

function playSound(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
if (!audio) return; //stop the function from running

audio.currentTime = 0;
audio.play(); 
audio.loop = false;
key.classList.add('playing'); };
function removeTransition(e) {
if (e.propertyName !=='transform')
    return;
this.classList.remove('playing');};
const keys = document.querySelectorAll('.key');
keys.forEach(keys => keys.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown',playSound);

0 个答案:

没有答案