Search code examples
javascripthtmlimagesrc

Changing src of image by iterating through a list


I am trying to have an image change every tenth of a second. I have written a script based off other responses on here and it is still not working. Here is the script:

var images = Array();
var index = 0;
images = ["rock.png", "paper.png", "scissors.png"];
var opponent = document.getElementById("opps");

setInterval(myMethod, 100);

function myMethod( ){
    opponent.src = images[index];

    if (index <= 1){
        index++;
    }
    else{
        index = 0;
    }
}

Here is the tag:

<img src = "" id ="opps"/>

Solution

  • I figured it out. To anyone else with this error it is due to having your script run before your html. Run script after your img.