将随机元素从一个数组移动到另一个数组

时间:2015-01-02 05:19:06

标签: javascript arrays

嘿所以我有一个包含8个声音的阵列,现在我已经拥有它所以当我点击一个图像时,一个随机声音从阵列播放,它移动到一个新的阵列,当原始的声音阵列是为空,然后新数组将歌曲传回原始数组。

-the problem is that when it removes the sound its not the sound that was just played.

-also id like to make clicking on the image multiple times not do anything other than playing the one sound until it finishes(then its moved to the empty array),then you can click the image for a new random sound

var sounds = [
"https://evolution.voxeo.com/library/audio/prompts/numbers/1.wav",
"https://evolution.voxeo.com/library/audio/prompts/numbers/2.wav",
"https://evolution.voxeo.com/library/audio/prompts/numbers/3.wav",
"https://evolution.voxeo.com/library/audio/prompts/numbers/4.wav",
"https://evolution.voxeo.com/library/audio/prompts/numbers/5.wav",
"https://evolution.voxeo.com/library/audio/prompts/numbers/6.wav",
"https://evolution.voxeo.com/library/audio/prompts/numbers/7.wav",
"https://evolution.voxeo.com/library/audio/prompts/numbers/8.wav"
];

var oldSounds = [];

function playSound()
{
var randomSound = sounds[Math.floor(Math.random() * sounds.length)];

document.getElementById("player").innerHTML=
"<embed src=\""+randomSound+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";

//splice randomSound from sounds array into var removed
//than push that sound into oldSounds array
var removed = sounds.splice(randomSound, 1);
oldSounds.push(removed);
console.log("==song removed from sound array = " + removed);
console.log(" .sounds length = " + sounds.length);
console.log(" .oldSounds length = " + oldSounds.length);

//if all sounds played from sound array AND all sounds are now in oldSounds array
//than move the sounds  from oldSounds to sounds
if (sounds.length === 0 && oldSounds.length === 8)
{
    console.log("----------------");
    sounds = oldSounds;
    console.log("sounds length = " + sounds.length);
    oldSounds = [];
    console.log("oldSounds length = " + oldSounds.length);
    console.log("----------------");
}
}

这是我到目前为止:http://jsbin.com/sekajumeva/1/edit?html,js,console,output

任何帮助将不胜感激。感谢。

3 个答案:

答案 0 :(得分:1)

问题应来自splice 它返回一个数组,当你推到oldSounds时,它将是数组中的数组,这不是你想要的?正确的将是&gt;&gt;

var removed = sounds.splice(randomSound, 1);
oldSounds.push(removed[0]);

问题2,你可以制作一个变量来保持状态 例如playing的状态。启动默认false的状态,例如var isPlaying = false。然后在你的playSound()函数中,你将检查状态,如果正在播放,只需返回。 if (isPlaying) {return;} else {isPlaying = true} ..请记住在函数playSound的末尾,将标志设置为false isPlaying= false

var isPlaying = false;
function playSound() {
  if (isPlaying) {
   return;
  }
  isPlaying = true; // set the state to true, if using click the image again, this function will not be called because returned on the `if` on top

  // ur original codes
  var randomSound = sounds[Math.floor(Math.random() * sounds.length)];
  document.getElementById("player").innerHTML=
    "<embed src=\""+randomSound+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";

  ....
  isPlaying = false; // set the state back to false, so the music will be played
}

答案 1 :(得分:0)

据我所知,splice()index作为第一个参数,然后传递声音网址。

要修复您的代码,请转换它:

var removed = sounds.splice(randomSound, 1);

进入这个:

var removed = sounds.splice(sounds.indexOf(randomSound), 1);

我没有测试过,因为说实话,我不能在这里听到任何声音,但看看它是否有效,新年快乐!

答案 2 :(得分:0)

所以你的代码是:

var sounds = [
    "https://evolution.voxeo.com/library/audio/prompts/numbers/1.wav",
    "https://evolution.voxeo.com/library/audio/prompts/numbers/2.wav",
    ...
];

var oldSounds = [];

function playSound()
{
    var randomSound = sounds[Math.floor(Math.random() * sounds.length)];

所以 randomSound 会像&#34; https://evolution.voxeo.com/library/audio/prompts/numbers/2.wav&#34;。然后:

    document.getElementById("player").innerHTML=
    "<embed src=\""+randomSound+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";

    //splice randomSound from sounds array into var removed
    //than push that sound into oldSounds array
    var removed = sounds.splice(randomSound, 1);

splice 的第一个参数应该是索引(0,1,2等),但是你传递的是上面的字符串。那将被转换为0(见Array.prototype.splice step 5),所以你只是继续拼接数组的第一个成员。

最好做一些事情:

var indexToPlay = Math.floor(Math.random() * sounds.length);
var randomSound = sounds[indexToPlau];

...

var removed = sounds.splice(indexToPlay, 1);