Solved

Javascript swap image

Posted on 2014-07-24
3
240 Views
Last Modified: 2014-07-24
Hello expert,

Got the code copied below to work, sort of.
It displays the first image and goes  to the second
but won't advance to the third.

Any idea why it is hanging?

Thanks

Allen in Dallas

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <!-- from http://stackoverflow.com/questions/3264739/image-change-every-30-seconds-loop -->
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
var images = new Array()
images[0] = "shared_vision_spirit.jpg";
images[1] = "shared_vision_clear.jpg";
images[2] = "shared_govern.jpg";
setTimeout("changeImage()", 1000);
var x=0;

function changeImage()
{
document.getElementById("img").src=images[x]
x++;
}
    </script>

    <img id="img" src="shared_vision.jpg">
  </body>
</html>

Open in new window

0
Comment
Question by:9apit
3 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40217321
Did you check your javascript error on developer tools ?

The third one image file might not  exist in the directory you
are running the web page

Click on the developer tools, and click console  that you will see the error if any
What browser are you using ?

Duncan
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40217331
That should be setInterval not setTimeout
And then you need a check if x is greater than the numer of array elements.
And it helps to markup your page correctly

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var images = new Array()
images[0] = "shared_vision_spirit.jpg";
images[1] = "shared_vision_clear.jpg";
images[2] = "shared_govern.jpg";
setInterval("changeImage()", 1000);
var x=0

function changeImage()
{

document.getElementById("img").src=images[x]
x++;
if (x>2) x = 0
}
    </script>
</head>
<body>
    <img id="img" src="shared_vision.jpg">
  </body>
</html>

Open in new window

0
 

Author Comment

by:9apit
ID: 40217347
Hello Gary,

Thanks.

Allen in Dallas
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question