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

Move a random element from one array to another

嘿,所以我有一个包含 8 个声音的数组,现在我有了它,所以当我点击一个图像时,数组中的随机声音播放,它移动到一个新数组,当原始数组sounds 为空则新数组会将歌曲传回原数组。

-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

如有任何帮助,我们将不胜感激。谢谢。

据我所知,splice()index 作为第一个参数,而您传递的是声音 url.

要修复您的代码,请将其转换为:

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

进入这个:

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

我还没有测试,因为老实说我听不到任何声音,但看看它是否有效,新年快乐!

所以你的代码是:

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 将类似于“https://evolution.voxeo.com/library/audio/prompts/numbers/2.wav”。那么:

    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);

问题应该出在splice
它 return 是一个数组,当你推送到 oldSounds 时,它会是数组中的数组,这不是你想要的吗?正确的应该是 >>

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

问题2,你可以做一个变量来保存状态。
例如 playing 的状态。初始化一个默认 false 的状态,例如 var isPlaying = false。然后在你的 playSound() 函数中,你将检查状态,如果它正在播放,就 return。 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
}