Solved

Javascript swap image

Posted on 2014-07-24
3
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

739 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