Skip to content Skip to sidebar Skip to footer

Cancel An If Statement If Another Statement Comes True

I have this function that plays different sounds to different keys. But if I press a key, then press another key right away, the previous sound will still play. My question is, ho

Solution 1:

This isn't really a matter of "stopping" the if statement because they are individually doing their job correctly. What you want to stop is the sound clip being played.

For this you can use the .pause() method, set the .currentTime to 0, or I believe you can also set the volume to 0 and just let it play out.

As mentioned in a comment, there are a few SO questions that may have already answered this. Is there a unique situation that isn't being answered in those?

Solution 2:

I think this works for you:

var e = document.getElementById('enklyd');
functioncheckKeyPressed(evt) {
    if (evt.keyCode == "81") { //q
        e.pause();
        e.currentTime = 0;
        e = document.getElementById('enklyd');
        e.play();
    }
    if (evt.keyCode == "87") { //w
        e.pause();
        e.currentTime = 0;
        e = document.getElementById('lyd1');
        e.play();
    }
    if (evt.keyCode == "69") { //e
        e.pause();
        e.currentTime = 0;
        e = document.getElementById('lyd2');
        e.play();
    }
    if (evt.keyCode == "82") { //r
        e.pause();
        e.currentTime = 0;
        e = document.getElementById('lyd3');
        e.play();
    }
    if (evt.keyCode == "84") { //t
        e.pause();
        e.currentTime = 0;
        e = document.getElementById('lyd4');
        e.play();
    }
    if (evt.keyCode == "89") { //y
        e.pause();
        e.currentTime = 0;
        e = document.getElementById('lyd5');
        e.play();
    }
}

Solution 3:

Actually, the issue is not regarding the if condition. But you can try the following code, I think this will work fine for you.

window.addEventListener("keydown", checkKeyPressed, false);

var keyMap = {
    "81": "enklyd", //q"87": "lyd1", //w"69": "lyd2", //e"82": "lyd3", //r"84": "lyd4", //t"89": "lyd5", //y
};

var prevPlayed = null, target = null, prevTarget = null;

functioncheckKeyPressed(evt) {

    prevTarget = document.getElementById(keyMap[prevPlayed])
    target = document.getElementById(keyMap[evt.keyCode]);

    if (prevPlayed !== null && prevTarget !== null)
        prevTarget.pause();

    if (keyMap[evt.keyCode] && target !== null) {
        target.currentTime = 0;
        target.play();
    }

    prevPlayed = evt.keyCode;
}

Solution 4:

You should use 'switch' instead of using 'if'.Just add a common class to all your elements

const yourDiv = document.querySelector('yourDiv');
window.addEventListener("keydown", checkKeyPressed, false);

functioncheckKeyPressed(evt) {
    yourDiv.currentTime = 0;
    switch(evt) {
        case'81':                
            document.getElementById('enklyd').play();
            break;
        case'87' :
            document.getElementById('lyd1').play();
            break;
        case'69' :
            document.getElementById('lyd2').play();
            break;
        case'82' :
            document.getElementById('lyd3').play();
            break;
        case'84' :
            document.getElementById('lyd4').play();
            break;
        case'89' :
            document.getElementById('lyd5').play();
            break;
        default :
            returnnull;
        }
}

Post a Comment for "Cancel An If Statement If Another Statement Comes True"