V
V
Vladimir Fedorov2015-09-29 21:41:04
PHP
Vladimir Fedorov, 2015-09-29 21:41:04

Where is the error in my randomizer?

I want to randomly play the sound.
Some have already answered me, but unfortunately all their ways just crashed my client.
I decided to do it my way.

var arr = ["#bet1-sound","#bet2-sound","#bet3-sound"];
      var rands = Math.floor(Math.random() * arr.length);
      if (sound == 'on') {
        $(arr[rands])[0].play();
      }

I tried to make an array with three sounds.
And then pull out a random value from there, but for some reason not a random sound is produced, but all at once.
<audio id="bet1-sound" src="/sounds/Stavka-1.mp3" preload="auto"></audio> 
<audio id="bet2-sound" src="/sounds/Stavka-2.mp3" preload="auto"></audio> 
<audio id="bet3-sound" src="/sounds/Stavka-3.mp3" preload="auto"></audio>

How everything is arranged initially, you can see here: jsfiddle.net/u40ukoL9
And here is how it was done in the original, but I'm not good at mastering it at all: jsfiddle.net/x6n0nu60

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Ineshin, 2015-09-29
@IonDen

I would do with the Ion.Sound plugin . Like this:

var names = ["Stavka-1", "Stavka-2", "Stavka-3"];

ion.sound({
    sounds: [
        {name: names[0]},
        {name: names[1]},
        {name: names[2]}
    ],
    volume: 1,
    path: "/sounds/",
    preload: true
});

var rand = Math.round(Math.random() * (names.length - 1));
ion.sound.play(names[rand]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question