Solved

Javascript swap image

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now