javascript - sounds keep on playing over each other -


i using mysound play sound depending on whether condition met. having problem sounds play on each other when conditions not met. here code ideas?

game.countanimals = function(playergo) {   var count = {bulls:0, cows:0};   game.counter = 1;   (var = 0; < playergo.length; i++) {     var digpresent = playergo.indexof(game.score[i]);     if (playergo[i] == game.score[i]) {       count.bulls++;     }     else if (digpresent>=0) {       count.cows++;     }     game.counter++     if (count.bulls === playergo.length && game.counter < 7){       mysound2 = new audio("sounds/freedom%20tastes%20good.mp3");       mysound2.play();      }     else if (count.bulls !== playergo.length && game.counter < 7) {       mysound1 = new audio("sounds/ass%20is%20dead.mp3");       mysound1.play();     }     else if (game.counter > 7 ){       mysound = new audio("sounds/kill%20you%20dog.mp3");       mysound.play();      } 

you should stop other sounds before playing new one. also, don't instantiate sounds in function, when needed - have 3 sounds, instantiate them when page loads, outside function.

so fixed snippet should this:

var mysound = new audio("sounds/kill%20you%20dog.mp3"); var mysound1 = new audio("sounds/ass%20is%20dead.mp3"); var mysound2 = new audio("sounds/freedom%20tastes%20good.mp3"); game.countanimals = function(playergo) {   var count = {bulls:0, cows:0};   game.counter = 1;   (var = 0; < playergo.length; i++) {     var digpresent = playergo.indexof(game.score[i]);     if (playergo[i] == game.score[i]) {       count.bulls++;     }     else if (digpresent>=0) {       count.cows++;     }     game.counter++     if (count.bulls === playergo.length && game.counter < 7){       stopallsounds();       mysound2.play();      }     else if (count.bulls !== playergo.length && game.counter < 7) {       stopallsounds();       mysound1.play();     }     else if (game.counter > 7 ){       stopallsounds();       mysound.play();      }  function stopallsounds() {     mysound.pause();     mysound1.pause();     mysound2.pause();     // rewind them     mysound.currenttime = 0;     mysound1.currenttime = 0;     mysound2.currenttime = 0; } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -