disable + reable sound
sound is played onclick. if button is pressed, the sound is disabled. if button is pressed again, sound is enabled again.
HTML
<head>
</head>
<body>
<button id="control">click here</button>
<script src="mouseclick.js"></script>
</body>
JavaScript
let soundDisabled = false;
let soundControl = document.getElementById("control");
function clickSound() {
var audio = new Audio('https://files.catbox.moe/z1ss03.mp3');
audio.volume = 0.5;
audio.play();
}
window.addEventListener('click', clickSound, false);
soundControl.onclick = function() {
if (soundDisabled === false) {
window.removeEventListener('click', clickSound, false);
soundControl.innerHTML = "sound off";
soundDisabled == true;
} else if (soundDisabled) {
window.addEventListener('click', clickSound, false);
soundControl.innerHTML = "sound on";
}
}