vendredi 19 juin 2020

random change class on existing list of element

the process should be: I control how many elements are present; I create a unique random array of those numbers; I use the random sequence to give a class to the corresponding element every 5 seconds. But it does not work. what did i do wrong?

html

<div class="wrapper-sx">
    <h4>...</h4>
    <h4>...</h4>
</div>

jquery

var items = $("#working .wrapper-sx").find("h4");
var number = items.length;
var arr = [];
while(arr.length < number){
    var r = Math.floor(Math.random() * number) + 1;
    if(arr.indexOf(r) === -1) arr.push(r);
};
console.log(arr);
for (i = 0; i < number; i++) {
    setInterval(function () {
        items.removeClass("active");
        items.eq(arr[i]).addClass("active");
        console.log(i);
    }, 5000);
};

thank u so much




Aucun commentaire:

Enregistrer un commentaire