samedi 5 mai 2018

Remove the current ( random ) image from list jQuery

I'm trying to use effect to show images randomly but when the first image is visible, I want to remove it from the list to prevent duplicate ( reparation )

I have been trying to do something as seen below, there's no error but it's not working. Hope someone could help me on this - I'm not good at jQuery

jQuery(function(){
    jQuery(function(){
    function random(n) {
        return Math.floor(Math.random() * n);
    }
    var usedImages = {};
    var usedImagesCount = 0;
    var transition_time = 1000;
    var waiting_time = 100;
    var images = $('div.block img');
    var n = images.length;
    var current = random(n);
    images.hide();
    images.eq(current).show();

    if (!usedImages[current]){

        usedImages[current] = true;
        usedImagesCount++;
        if (usedImagesCount === images.length){
            usedImagesCount = 0;
            usedImages = {};
        }
    } else {
            var interval_id = setInterval(function () {
        images.eq(current).fadeOut(transition_time, function () {
            current = random(n);
            images.eq(current).fadeIn(transition_time);
        });
    }, 2 * transition_time + waiting_time);
    }


})

})




Aucun commentaire:

Enregistrer un commentaire