User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 373,930 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,222 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 861 | Replies: 2 | Solved
Reply
Join Date: Feb 2007
Posts: 52
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Difficulty with forms

  #1  
Nov 23rd, 2007
Hello, I am getting an error message saying clock is undefined, I am not sure why, here is my code:

  1. <script type ="text/javascript">
  2. var theDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  3. var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  4. var birthYear = 1960;
  5. var birthDay = 1;
  6. var birthMonth = 1;
  7. var retirementYear;
  8. var retirementAge = 65;
  9. var timerId;
  10. var firstDate = new Date();
  11. var lastDate = new Date();
  12. var minutes = 1000 * 60;
  13. var hours = minutes * 60;
  14. var days = hours * 24;
  15. var years = days * 365;
  16.  
  17. function startClock()
  18. {
  19. birthYear = prompt("Type the 4-digit year of your birth.");
  20. birthMonth = prompt("What month? (1-12)");
  21. birthDay = prompt("What day? (1-31)");
  22. retirementAge = prompt("At what age to you plan to retire?");
  23. retirementYear = birthYear + retirementAge;
  24. firstDate = new Date("birthYear", "birthMonth", "birthDay");
  25. lastDate = new Date("retirementYear", "birthMonth", "birthDay");
  26. timerId = setInterval("showTime()", 1);
  27. }
  28.  
  29. function stopClock()
  30. {
  31. clearInterval(timerId);
  32. }
  33.  
  34. function showTime()
  35. {
  36. var now = new Date();
  37. var seconds = ((now.getTime() + (retirementAge * 31556926000)) - now.getTime()) / 1000;
  38. var birthDateDisplay = "" + theDays[now.getDay()] + "," + birthDay + " " + theMonths[birthMonth] + ", " + birthYear;
  39. var retirementDisplay = "" + theDays[now.getDay()] + "," + birthDay + " " + theMonths[birthMonth] + ", " + (birthYear + retirementAge);
  40. clock.txtSecondsFace.value = seconds;
  41. clock.txtMinutesFace.value = seconds / 60;
  42. clock.txtHoursFace.value = (seconds / 60) / 60;
  43. clock.txtDaysFace.value = (seconds / 60 / 60) / 24;
  44. clock.txtYearsFace.value = (seconds / 60 / 60 / 24) / 365;
  45. clock.txtWorkDays.value = retirementForm.txtYearsFace.value * .65;
  46. clock.txtFunDays.value = retirementForm.txtYearsFace.value * .35;
  47. clock.txtBirthDate.value = birthDateDisplay;
  48. clock.txtRetirementDate.value = retirementDisplay;
  49. }
  50.  
  51. </script>
  52.  
  53.  
  54. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  55. <title>Retirement</title>
  56. </head>
  57.  
  58. <body>
  59.  
  60. <h1>Retirement Countdown</h1>
  61. <br />
  62. <form name = "clock" action = "">
  63. <p>
  64. Click the Start Clock button to begin:
  65. <input name = "btnStartClock" value = "Start Clock" type = "button" onclick = "startClock()" />
  66. <input name = "btnStopClock" value = "Stop Clock" type = "button" onclick = "stopClock()" />
  67. </p>
  68. <p>
  69. Birth Date:
  70. <input name = "txtBirthDate" type = "text" />
  71. </p>
  72. <p>
  73. Retirement Date:
  74. <input name = "txtRetirementDate" type = "text" />
  75. </p>
  76. <p>
  77. <h3>Countdown to Retirement</h3>
  78. </p>
  79. <p>
  80. Seconds:
  81. <input name = "txtSecondsFace" type = "text" />
  82. Minutes:
  83. <input name = "txtMinutesFace" type = "text" />
  84. </p>
  85. <p>
  86. Hours:
  87. <input name = "txtHoursFace" type = "text" />
  88. Days:
  89. <input name = "txtDaysFace" type = "text" />
  90. Years:
  91. <input name = "txtYearsFace" type = "text" />
  92. </p>
  93. <p>
  94. <h3>Work Days Versus Fun Days</h3>
  95. </p>
  96. <p>
  97. A typical full time employee works 65 days out of every 100 days.
  98. </p>
  99. <p>
  100. Work days left:
  101. <input name = "txtWorkDays" type = "text" />
  102. Fun days before retirement:
  103. <input name = "txtFunDays" type = "text" />
  104. </p>
  105. </form>
  106. <p>
  107. <h2>Now that is something to think about!</h2>
  108. </p>
My Website <-- check out my site!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Posts: 52
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: Difficulty with forms

  #2  
Nov 23rd, 2007
How do I Delete a thread?
My Website <-- check out my site!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Difficulty with forms

  #3  
Nov 23rd, 2007
A lot of problems to begin with.

» You are accessing form elements the wrong way. Read this.

» prompt() returns a string and you are treating it as a number which in itself is introducing a lot of problems.

» setTimeout() takes it's second parameter as milliseconds and not seconds.

» I see a lot of needless calculations going on in showTime() function. Don't touch the DOM unless absolutely necessary. For eg. since we know that the value of the 'hour' text field would only change after 60 minutes, there is no point in updating it every second.

» In javascript, numbers are represented using 64 bit double precision format as per the IEEE floating point standards. Showing the amount of 'seconds' left in decimals would be not desirable. Use the toFixed() function of Number to get away with those extra digits after the dot.

Follow the suggestions, format and indent the code and come up with a fresh solution.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 5:57 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC