Solution 1 :

You should call random generator too in each call

function ChangeImgSrc() {
    var RndomFrom0To3 = Math.floor(Math.random()*4);
    p.setAttribute('src',Src[RndomFrom0To3]);


    };

Problem :

im trying to show random image every 1 sec its an image inside a div without src attribute :

<div class="D">

<img>   

</div>

here is the css code :

 .D{

  height: 30%;
  width: 30%;
  left: 35%;
  top: 10%;
  position: absolute;

  }
  .D img{

  position: absolute;
  height: 100%;
  width: 100%;

  }

i have an array that contains src of 4 diferrent images from the folder and with Math random im picking a random number from 0 to 3 as the array index and repeat the function with setinterval :

var inp = document.querySelector('.D');
var p = document.querySelector('.D img');
var Src= ['Imgs/home.png','Imgs/gps.png','Imgs/bell.png','Imgs/box.png'];
var RndomFrom0To3 = Math.floor(Math.random()*4);


function ChangeImgSrc() {

p.setAttribute('src',Src[RndomFrom0To3]);


};


setInterval(ChangeImgSrc,1000);

the src changes but once and it stop in the same image what im i doing wrong ?

Comments

Comment posted by RobG

var RndomFrom0To3 = Math.floor(Math.random()*4);

By

Leave a Reply

Your email address will not be published. Required fields are marked *