Timer function help

got an answer from @lassekorsgaard

let currentSongIndex = 0
let totalSongs = 6
let canPlay = true
let timer = null

function playSong() {
    if (timer) {
        clearTimeout(timer)
        timer = null
    }

    songs[currentSongIndex].play()
    timer = setTimeout(nextSong, 5000)
}

function nextSong() {
    if (currentSongIndex < totalSongs) {
        currentSongIndex++
    }else{
        currentSongIndex = 0
    }

    playSong()
}


function speechResult() {
    var resultArray = myRec.resultString.split(' ').pop()

    if (resultArray.indexOf('') > -1 && canPlay) {
        nextSong()
    }
}
1 Like