Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript swap image

Posted on 2014-07-24
3
Medium Priority
?
245 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:Allen Pitts
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 2000 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:Allen Pitts
ID: 40217347
Hello Gary,

Thanks.

Allen in Dallas
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

963 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