JQuery mots aléatoires sans répéter

Je dois afficher des mots aléatoires dans div sans répéter le mot. Les mots aléatoires appendont un div toutes les secondes aléatoires (3-5 secondes). Si toute la valeur du tableau affiché dans div, alert fera l’affaire.

Exemple:

b a c d ALERT('DONE') 

Ne pas:

 b a b c d d a a c 

Mon code:

 $(document).ready(function($) { words = ['a','b','c','d']; function doSomething() {} (function loop() { var rand = Math.round(Math.random() * (3000 - 500)) + 500; setTimeout(function() { var thisWord = words[Math.floor(Math.random() * words.length)]; $("#container").append("
"+thisWord+"
"); doSomething(); loop(); }, rand); }()); });

Voici une solution de travail:

 $(document).ready(function() { var words = ['a', 'b', 'c', 'd']; var getRandom = function() { var idx = Math.floor(Math.random() * words.length); // grabs word and removes it from the array return words.splice(idx, 1)[0]; }; var appendIfMore = function() { var word = getRandom(); if (!word) return; // all done $('
') .text(word) .appendTo('#container'); var delay = Math.round(Math.random() * (3000 - 500)) + 500; setTimeout(appendIfMore, delay); }; // start appending stuff appendIfMore(); });

JSBIN