mercredi 25 novembre 2015

How to get this Code correctly into for loops?

JavaScript looks something like that:

function playsound(button) {

switch(button){
case "button1":
    switch(soundbank){
        case 0:
            document.getElementById("audio1").load();
            document.getElementById("audio1").play();
            break;
        case 1:
            ...
            ...
case "button2":
    switch(soundbank){
        case 0:
            document.getElementById("audio2").load();
            document.getElementById("audio2").play();
            break;
case "button3":
    switch(soundbank){
        case 0:
            document.getElementById("audio3").load();
            document.getElementById("audio3").play();
            break;
case "button4":
    switch(soundbank){
        case 0:
            document.getElementById("audio4").load();
            document.getElementById("audio4").play();
            break;
case "..."
    ...
    ...
}}

HTML like this:

<button onclick="playsound(this.id)" id="button1" </button>
<button onclick="playsound(this.id)" id="button2" </button>
<button onclick="playsound(this.id)" id="button3" </button>
<button onclick="playsound(this.id)" id="button4" </button>

<audio id="audio1">
    <source src="audio1.wav">
</audio>
<audio id="audio2">
    <source src="audio2.wav">
</audio>
<audio id="audio3">
    <source src="audio3.wav">
</audio>
<audio id="audio4">
    <source src="audio4.wav">
</audio>

I would like to put most of it into loops instead of having 1000 lines of switch case and If Else but I can't work it out. Any Suggestions? Thanks a lot.

Aucun commentaire:

Enregistrer un commentaire