samedi 17 août 2019

How do we assign the URL to the respective image which is randomly appearing?

The aim is to assign the respective URL to the respective image which is randomly appearing every 1 second on a web page in J Query.

so was wondering how do i combine both random items together? I probably did the wrong way of combining them by concatenation. hope to get some help here!


<script>
var img_arr = ["adidas_logo.jpg", "apple_logo.jpg","Changi_airport.jpg","starbucks.jpg"]

var links_arr =["https://www.adidas.com.sg/" , "https://www.apple.com/sg/" , "https://www.changiairport.com/","https://www.starbucks.com.sg/"]

var current = 0;
var current1 = 0;

setInterval(function()
{   
    var pic = Math.floor(Math.random()*img_arr.length);
    var links = Math.floor(Math.random()*links_arr.length);

    $("#image_slider").attr("src",img_arr[pic] +" "+ links_arr[links]);

    current = (img_arr[pic]< img_arr.length -1)? img_arr[pic] + 1 : 0;
    current1 = (img_arr[pic]< img_arr.length -1)? links_arr[links] + 1 : 0;

},1000);

the expected output is the random images displayed when clicked , it will go to the correct URL. currently my error is it is random but the img src and URL are combined so the image doesn't appear.




Aucun commentaire:

Enregistrer un commentaire