-1

One of the case statements in the top function below is not going in order. Currently, it goes straight to the prompt, and if you click Cancel, operatorPickup is never heard at all. If I type 'police', I get the police recording which is in a different function called at the end of the current function and then I get OperatorAnswer which makes absolutely no sense which it is going in the order 2, 3, 1 - when 3 is not even in the same function!

From previous experience, I tried delaying the prompt in case alerts, prompts, or confirms blocked the UI and took precedence, but that didn't help either.

function response() {
        switch(number) {
            case "0":
                var operatorPickup = new Audio('OperatorAnswer.wav');
                operatorPickup.play();
                setTimeout(function() {
                    number = prompt("Operator, your number please? (Numbers only; enter 'police' for police and emergency)");
                    number = number;
                    operatorPutCallThrough();
                }, 9);
                break;
            case "911":
                var pickup911 = new Audio('911-xxx-fleet.mp3');
                pickup911.play();
                break;
            case "18477651008":
                var pickupMCI = new Audio('MCI.wav');
                pickupMCI.play();
                break;
            case "8675309":
                var pickup8675309 = new Audio('discoornis-bell-f1.mp3');
                pickup8675309.play();
                break;
            case "police":
                break;
            default:
                var pickupDefault = new Audio('ldcircuits-bell-f1.mp3');
                pickupDefault.play();
        }
    }
    function operatorPutCallThrough() {
        if (number == "police") {
            var operatorPoliceTransfer = new Audio('OperatorPolice.wav');
            operatorPoliceTransfer.play();
        }
        response();
    }

How can I force case "0" in switch(number) to execute in order?

InterLinked
  • 1,247
  • 2
  • 18
  • 50

2 Answers2

1

you could use an event listener on the play() so it will only prompt once media file finished playing.

I've never worked with HTML5's audio as of yet but this question: Detecting when HTML5 audio is finished playing (more than once)? suggested how you can listen to the event so you can try:

operatorPickup.addEventListener("ended", function(){
    number = prompt("Operator, your number please? (Numbers only; enter 'police' for police and emergency)");
    number = number;
    operatorPutCallThrough();
});

BEFORE the operatorPickup.play();

Community
  • 1
  • 1
Jameson the dog
  • 1,796
  • 1
  • 11
  • 12
  • If I do this, no recording plays at all, but the alert box doesn't pop up. Everything just stops – InterLinked May 18 '17 at 11:38
  • that doesn't make sense... do you have any errors in the console? did you add the eventlistener between `var operatorPickup = new Audio('OperatorAnswer.wav');` and `operatorPickup.play();` ? – Jameson the dog May 18 '17 at 11:50
  • I had it before the var operatorPickup define too, that solved it! – InterLinked May 18 '17 at 12:15
1

If you click cancel, the variable number gets set to null, which isn't in the case statements. If you want to go to the same case on null and 0, you should add case null immediately above or below case "0". If there isn't a break before the next case, the switch-statement will execute the next case and so on, so you can stack case statements without breaks over the code that should execute for any one of those cases.

Haem
  • 929
  • 6
  • 15
  • 31
  • Hmm... I tried setting var previousNumber = number and then after case 0 runs changing number to previousNumber if number is null... but consoleLog is reporting nothing for any of them... – InterLinked May 18 '17 at 12:20