0

I am really new to JavaScript and working on a project to replace an image every 5 seconds without using a onClick command.

The first function startAdPage should start when the page loads, allowing the setInterval commands inside that function to begin. Once the changeAd function starts it is supposed to replace the image in the table with the matching id.

I would really appreciate some advice. Some that may be a problem which I am not sure about is I have the Javascript contained in the body of my document, but the onLoad at the start of the body. Is that an issue as well?

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CVR1</title>
</head>
<body onLoad="startAdPage()">
<script type="text/javascript">
/* <![CDATA[ */
function startAdPage() {
	setInterval(changAd,5000);
}
function changeAd() { 
	//THIS NEEDS WORK should change the image every 5 seconds to replace the one in the table
	document.getElementById("adImage").src = images[index];
	index = (index + 1) % images.length;
}
/* ]]> */
</script>
<table>
  <tr>
    <td><img id="adImage" src="pictures/cvb1.gif" ></td>
    <td><p>Advertisement</p><p>The Central Vally Realtors home page will be displayed in TEXTFIELD seconds.</p><p><a href="CVR2.html">Skip Advertisement</a></p></td> 
  </tr>
</table>
</body>
</html>

2
  • how do you get data for images variable? is it undefined? Commented Oct 17, 2014 at 6:29
  • You never initialized index to 0. And where is the definition of images? Commented Oct 17, 2014 at 6:29

1 Answer 1

3

On this line:

setInterval(changAd,5000);

changAd should be changeAd

Remember to press F12 and check the console for errors, you have:

Uncaught ReferenceError: changAd is not defined

As the comments show there also seem to be other essential parts missing: the images array and index is undefined.

var index = 0;

var images = [
    'image1.png',
    'image2.png'
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.