javascript - How to go through an unknown number of quotes randomly without repeating -


i have small snippet of javascript rotate through list of quotes in order beginning end.

however, want randomly go through list (instead of in order), without repeating until of quotes iterated through, , start around random quote again. how go doing this?

$(function(){     var quotes = $('#quotes').children('.rotate-quote');     firstquo = quotes.filter(':first');     lastquo = quotes.filter(':last');     quotes.first().show();     setinterval(function(){         if($(lastquo).is(':visible')) {             var nextelem = $(firstquo);         } else {             var nextelem = $(quotes).filter(':visible').next();         }         $(quotes).filter(':visible').fadeout(300);         if($(lastquo).is(':visible')) {             settimeout(function() {                 $(firstquo).fadein(300);             }, 600);          } else {             settimeout(function() {                 $(nextelem).fadein(600);             }, 600);         }     }, 10000); }); 

here's possible solution demo:

var $container = $('div'),     quotes = $('quote').hide().toarray(),     delay = 500;  function shuffle(arr) {   return arr.map(function(v){ return [v,math.random()]; })     .sort().map(function(v){ return v[0]; }); }  function loop() {   $(shuffle(quotes)).each(function(i,el) {     settimeout(function(){ $(el).appendto($container).show(); }, i*delay);   }); }  function start() {   function begin(){ $(quotes).hide(); loop(); }   setinterval(begin, quotes.length * delay);   begin(); }  start(); 

demo: http://jsbin.com/agihix/1/edit

edit: turned little plugin, grab here https://gist.github.com/elclanrs/5610886


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -