473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Traffic Light illumination using an array

Hi There

This is my first time on the site. I am studing information technology and one of my subjects is HTML & Javascript. I am battling and have an assignment due, please help!

I need to use cycle & stop buttons to control the animation and javascript code to animate the images of a traffic light. I have drawn 3 pictures in Paint, each with a different colour illumated and saved as banner1.jpg, banner2.jpg & banner3.jpg respectively.
The screen layout is fine but I cannot get the cycle and stop function to work, there is obviously something very wrong in my code and being a "newbie" have no idea where to find the problem.
Any help will be appreciated.

My code so far:
<html>
<head>
<title>Question 3 Traffic Lights</title>

<SCRIPT LANGUAGE="JAVASCRIPT>
<!-- Hide from old browsers

var banners = new Array("banner1.jpg","banner2.jpg","banner3.jpg")
var bnrCntr = 0
var timer
function bancycle() {
bnrCntr = bnrCntr + 1
if (bnrCntr == 3) {
bnrCntr = 0
}
document.Banner.src = banners[bnrCntr]
timer = setTimeout("bancycle()",1000)
}
function stopcycle() {
clearTimeout(timer)
}

//-->
</script>
</head>
<body>
<img src="banner1.jpg" name="banner" width=110 height=200>
<form><input type="button" value="Cycle" name="Cycle" onclick="banCycle()">
<input type="button" value="Stop" name="Stop" onclick="stopCycle()">
</form>
</body>
Jun 16 '07 #1
9 18378
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Javascript is case-sensitive. "Banner" is not equal to "banner".
Jun 16 '07 #2
Thanks . I changed the case but it still will not cycle and stop.
Attached updated code.... please assist.

<html>
<head>
<title>Question 3 Traffic Lights</title>

<SCRIPT LANGUAGE="JAVASCRIPT>
<!-- Hide from old browsers

var banners = new Array("banner1.jpg","banner2.jpg","banner3.jpg")
var bnrCntr = 0
var timer
function banCycle() {
bnrCntr = bnrCntr + 1
if (bnrCntr == 3) {
bnrCntr = 0
}
document.banner.src = banners[bnrCntr]
timer = setTimeout("banCycle()",3000)
}
function stopcycle() {
clearTimeout(timer)
}

//-->
</script>
</head>
<body>
<img src="banner1.jpg" name="banner" width=110 height=200>
<form><input type="button" value="Cycle" name="Cycle" onclick="banCycle()">
<input type="button" value="Stop" name="Stop" onclick="stopCycle()">
</form>
</body>
Jun 16 '07 #3
Logician
210 100+
Hi There

This is my first time on the site. I am studing information technology and one of my subjects is HTML & Javascript. I am battling and have an assignment due, please help!

I need to use cycle & stop buttons to control the animation and javascript code to animate the images of a traffic light. I have drawn 3 pictures in Paint, each with a different colour illumated and saved as banner1.jpg, banner2.jpg & banner3.jpg respectively.
The screen layout is fine but I cannot get the cycle and stop function to work, there is obviously something very wrong in my code and being a "newbie" have no idea where to find the problem.
Any help will be appreciated.
JavaScript Lesson #1
---------------------------------------------------
Always check the JavaScript console.
---------------------------------------------------

Your buttons are calling functions different to those you have written.
<!-- --> comments in scripts ary no longer needed.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type='text/javascript'>
  4. var banners = ["banner1.jpg","banner2.jpg","banner3.jpg"];
  5. var bnrCntr = 0;
  6. var timer;
  7. function banCycle()
  8. {
  9.  if(++bnrCntr == 3)
  10.   bnrCntr = 0;
  11.  
  12.  document.images.banner.src = banners[bnrCntr];
  13.  
  14.  timer = setTimeout("banCycle()",1000);
  15. }
  16. function stopCycle()
  17. {
  18.  clearTimeout(timer);
  19. }
  20.  
  21. </script>
  22. </head>
  23. <body>
  24. <img src="banner1.jpg" name="banner" width=110 height=200>
  25.  
  26. <form>
  27.  <input type="button" value="Cycle" name="Cycle" onclick="banCycle()">
  28.  <input type="button" value="Stop" name="Stop" onclick="stopCycle()">
  29. </form>
  30. </body>
  31. </html>
  32.  
Jun 16 '07 #4
Thanx Logician, it worked like a charm.
Your comment re <!--> is noted however, the lecturer wants us to enter in this format!

Watch this space as the next item I am battling with is my code for a digital clock. If you have a spare moment feel free to comment, all the help I can get is always appreciated!

This is what I have so far:
<html>
<head>
<title>Digital Clock</title>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function curTime() {
var now = new date();
var day = now.GetDay();
var date = now.GetDate();
var year = now.GetFullYear();
var month = now.GetMonth();
var hours = now.GetHours() + 1;
var minutes = now.GetMinutes();
var seconds = now.GetSeconds();
var days = new Array();
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thursday";
days[5] = "Friday";
days[6]="Saturday";
var display=days[day] + " " + month + "/" + date + "/"
+ year + " "+ hours + ":" + minutes + ":" + seconds;
document.forms[0].readout.value=display;
}
var tick = setInterval("curTime()", 1000);
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<form action="">
<p><input type="text" size="30" name="readout" /></p>
</form>
</body>
</html>
Jun 16 '07 #5
Logician
210 100+
Thanx Logician, it worked like a charm.
Your comment re <!--> is noted however, the lecturer wants us to enter in this format!
Then tell him he needs to get a more recent book. Any browser that needs those can't handle today's websites and they are potentially harmful in XHTML, where they really can hide a script.
Watch this space as the next item I am battling with is my code for a digital clock. If you have a spare moment feel free to comment, all the help I can get is always appreciated!

This is what I have so far:
<snip> There's nothing wrong there that can't be detected by following lesson #1 (Use FireFox).
Jun 16 '07 #6
drhowarddrfine
7,435 Expert 4TB
Tell him he needs a proper doctype, too. See the article under "Articles" at the top under HTML/CSS.
Jun 16 '07 #7
Tell him he needs a proper doctype, too. See the article under "Articles" at the top under HTML/CSS.
Will do - in fact it's a she.

So what is this firefox all about. Do I download software and this helps check my code??

For the studies I am doing it is supposed to be simple stull that we save as htm files but never get published etc.

I just have to complete the assignment questions and send in for marking by the 23rd. If you can help me with this code in the meantime will appreciate.
Jun 16 '07 #8
acoder
16,027 Expert Mod 8TB
Maybe you need a Javascript tutorial. Firefox is an alternative browser to Internet Explorer. It's a lot more helpful when debugging, not to mention more standards-compliant.
Jun 18 '07 #9
Hi,i was just wondering if you know what structure of an array is used to handle the traffic light sequence.Im asking as this is the same project I'm doing and I'm so stuck on this part.Thankyou.
Oct 8 '16 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bla | last post by:
Hi, There is a new Yahoo group called " Traffic-Cams " This group discusses everything related to online and realtime traffic-cameras We have a very extensive links-section with links to...
6
by: Mirek | last post by:
Hi, The application setup is: Access frontend + ODBC (TCP/IP) + MSSQL7 Few days ago i was informed that the app generates high network traffic. Thus i started to look for some savings. I...
2
by: macca | last post by:
Hi, I would like to create a control that looks like a set of traffic lights. This will output alarm states and if the user clicks on the control it will open up a dialog that gives more...
26
by: Wouter van Teijlingen | last post by:
Dear Readers, This is my first post to this group. I was pointed to this group in a other vb group, so i have better luck here! For my learning curve of VB .NET i want to make a traffic...
2
by: matt | last post by:
Does anyone know of a service, that provides driving traffic information in the form of xml? I see several large cities have information on the web, including traffic cameras. traffic.com has an...
4
by: jaja99 | last post by:
hello to all.. PLEASE HELP ME!! i wanted to make a traffic light in vb using if then else.. 3 circles (red, yellow & green) and a timer i don't know how to make them appear one by one.....
2
by: juvilan | last post by:
I need a code for my thesis and I don’t know how to create a VB code for the traffic light.
1
by: davidjacob | last post by:
any program code in vb studio that can help in designing traffic light is appreciated thanks
8
by: razer | last post by:
Hi everybody I'm making a project where i need to make a simulation of a traffic light. The cars appear randomly, and are represented by letter, the traffic light stays open for x seconds, then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.