|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Can anybody see the error(s) in this code?
I have to make a page that will display a picture (in this case a traffic light), with a start and stop button underneath. When you click the start button, the program is supposed to loop displayed pictures to make it seem like the picture is animated.
This is what I have done: Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Project_11-TrafficLight</title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */var StartChanging;
var light = new Array(3);
var curLight = 0;
for (var imagesLoaded=0; imagesLoaded < 3; ++imagesLoaded) {
light[imagesLoaded] = new Image();
light[imagesLoaded].src = "light"
+ imagesLoaded + ".gif";
}
function turn() {
if (curLight == 2)
curLight = 0;
else
++curLight;
document.images[0].src = light[curLight].src;
}
function startChanging() {
if (StartChanging != null)
clearInterval(StartChanging);
StartChanging = setInterval("turn()", 47);
}
/* ]]> */
</script>
</head>
<body>
<p><img src="green.gif" alt="Green Traffic Light" height="110" width="80" /></p>
<form action="">
<p><input type="button" value=" Change "
onclick="startChanging();" />
<input type="button" value=" Stop "
onclick="clearInterval(StartChanging);" /></p>
</form>
</body>
</html>
The browser doesn't tell me there's any errors..and I don't know what's wrong with it. |
|
#2
|
|||
|
|||
|
I'd check your images- the timer works, as you can see by writing to a div each time the src is supposed to change-
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Project_11-TrafficLight</title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */var StartChanging;
var light = new Array(3);
var curLight = 0;
for (var imagesLoaded=0; imagesLoaded < 3; ++imagesLoaded) {
light[imagesLoaded] = new Image();
light[imagesLoaded].src = "light"
+ imagesLoaded + ".gif";
}
function turn() {
if (curLight == 2)
curLight = 0;
else
++curLight;
document.images[0].src = light[curLight].src;
document.getElementById('count').innerHTML+=document.images[0].src+'<br>';
}
function startChanging() {
if (StartChanging != null) clearInterval(StartChanging);
StartChanging = setInterval("turn()", 47);
}
/* ]]> */
</script>
</head>
<body>
<p><img src="green.gif" alt="Green Traffic Light" height="110" width="80" /></p>
<form action="">
<p><input type="button" value=" Change "
onclick="startChanging();" />
<input type="button" value=" Stop "
onclick="clearInterval(StartChanging);" /></p>
</form>
<div id='count'>
0
</div>
</body>
</html>
|
|
#3
|
|||
|
|||
|
try
Code:
document.images[0] = light[curLight]; Code:
document.images[0].src = light[curLight].src; Sometimes programming languages can be pretty picky. The problem with javascript is that errors often end up getting quietly suppressed. see if you can view error reports in your browser - even if all you can get from these is the line number where the error occured, this can still be very useful. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|