I need to display random words in div without repeating the word. The random words will append a div every random seconds (3-5 seconds). If all value in array displayed in div,
Here's a working solution:
$(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
$('<div class="conversation"/>')
.text(word)
.appendTo('#container');
var delay = Math.round(Math.random() * (3000 - 500)) + 500;
setTimeout(appendIfMore, delay);
};
// start appending stuff
appendIfMore();
});
JSBIN