javascript - Change images randomly on click without repeating -


i found code on stack overflow: change images on click.

and works me. want random, how can prevent repeating images. should first repeat images, when user has clicked through images.

i have made jsfiddle code: http://jsfiddle.net/gr3f4hp1/

jquery code:

var images = ["02.jpg","03.jpg","01.jpg"];  $(function() {     $('.change').click(function(e) {     var image = images[math.floor(math.random()*images.length)];         $('#bg').parent().fadeout(200, function() {             $('#bg').attr('src', 'items/'+image);                $(this).fadein(200);         });     }); }); 

keep track of numbers have generated, , if repeat new number.

you can work around

var usedimages = {}; var usedimagescount = 0;  function displayimage(){      var num = math.floor(math.random() * (imagesarray.length));     if (!usedimages[num]){         document.canvas.src = imagesarray[num];         usedimages[num] = true;         usedimagescount++;         if (usedimagescount === imagesarray.length){             usedimagescount = 0;             usedimages = {};         }     } else {         displayimage();     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -