Solved

Javascript swap image

Posted on 2014-07-24
3
234 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now