jeudi 24 juin 2021

How do i acces a link from array in a random order without repeating the last one chosen?

  let urlArray = [
            "link",
            "link2",
            "link3",
        ];
        function randomUrl() {    

            let randomNumber = Math.floor(Math.random() * urlArray.length);
            let newUrl = urlArray[randomNumber];
            let visitedGame = document.referrer;
            setTimeout(function () {
                //if the previous link is not the same as the one generated access the generated link
                if (newUrl !== visitedGame)  {
                    window.location = newUrl;
               //access the next link from the array
                 } else{
                    
                 }
            }, 3000);
        }
        randomUrl();

I have this code that i'm stuck at.

So what i'm trying to do:

On some click ( that is not in the code right here, but it works ) i access this function. When i click that button, i need to generate a random link from this array, but without repeating the last accesed in the browser. so without the previous url that was accesed. I tried getting the document. referrer but it seems it doesn't work. Please help :(




Aucun commentaire:

Enregistrer un commentaire