How to add time to this code! Automatically switch to the next random picture

< script language= "JavaScript" type= "text/javascript" >
/ / Random image code
tips = new Array (3);
tips [0] =" ";
tips [1] =" ";
tips [2] =" index = Math.floor (Math.random () * tips.length);
document.write (tips [tips.length]);
< / script >

how to add time to this code! Automatically switch to the next random picture

May.22,2021


< html lang= "en" >
< head >

<meta charset="UTF-8">
<title>Document</title>

< / head >
< body >

<img id='img' src="" alt="">
<script>
    window.onload = function () {
        var imgDom = document.getElementById('img')
        var imgArr = ['tiggo32.png','tiggo32.png','tiggo32.png']

        function setImg (){
            var index = Math.floor(Math.random() * tips.length)
            imgDom.src = imgArr[index]
        }

        setImg()
        setInterval( function (){
            setImg()
        }, 5000)

    }
</script>

< / body >
< / html >


you shouldn't write like this, you're thinking wrong.

var a = ["x.jpg", "y.jpg", "q.jpg"];
var img=new Image ();
setInterval (function () {

         img.src=a[Math.floor(Math.random()*a.length)];

document.getBytabNames ("body") [0] .addChilen (img)

Menu