停止音频阵列循环



我正在尝试构建一个鼓定序器,并从一个可以连续循环的单个音频阵列开始
我想双击停止音频,这是我用停止功能完成的。然而,当查看控制台时,计数器仍在递增。我试着用break来阻止这种行为,但被告知这是非法的。

这是代码,感谢您的帮助。

const drumSequencer = [true, false, false, true, true, false, false, false];
const audio = document.getElementById('soundOne');

function start() {
let counter = 0;
setInterval(function(){ 
if (drumSequencer[counter] === true) { 
audio.play().catch(function(error) {
console.log('error', error)
})}
console.log(counter);
counter++;
//loops through array
if (counter === 8) {
counter = 0;
}
//stops array if double click on screen
if (document.addEventListener().dblclick) {
break;
}
}, 1000);}

function stop() {
audio.pause();
audio.currentTime = 0;
}
//starts audio
document.addEventListener('click', start)
//stops audio
document.addEventListener('dblclick', stop)
const drumSequencer = [true, false, false, true, true, false, false, false];
const audio = document.getElementById('soundOne');
let stopped = false;
let counter = 0;
function restart(){
counter = 0;
stopped = false;
start();
}
function start() {
setTimeout(function(){ 
if (drumSequencer[counter] === true) { 
audio.play().catch(function(error) {
console.log('error', error)
})}
console.log(counter);
counter++;
//loops through array
if (counter === 8) {
counter = 0;
}
//stops array if double click on screen
if (stopped==false) {
start();
}
}, 1000);}

function stop() {
audio.pause();
audio.currentTime = 0;
stopped = true;
}
//starts audio
document.addEventListener('click', restart)
//stops audio
document.addEventListener('dblclick', stop)

我已经编辑了你的代码。请检查

最新更新