Connecting Tech Pros Worldwide Forums | Help | Site Map

how to apply javascript without any event

Member
 
Join Date: Jan 2008
Location: dhanbad
Posts: 70
#1: Feb 7 '08
hi....
i want to display current date and time of client side in the page. i have the javascript code also bt i dnt know how to use it in the page. the code is

Expand|Select|Wrap|Line Numbers
  1. function ShowTime()
  2. {
  3. var TimerKey
  4. var now = new Date()
  5. var Hours = now.getHours()
  6. var Minutes = now.getMinutes()
  7. var Seconds = now.getSeconds()
  8. TimeDisplay.innerText = ((Hours > 12) ? Hours - 12 : Hours) + ((Minutes < 10) ? ":0" : ":") + Minutes + ((Seconds < 10) ? ":0" : ":") + Seconds + ((Hours > 12) ? " PM" : " AM")
  9. window.status = ((Hours > 12) ? Hours - 12 : Hours) + ((Minutes < 10) ? ":0" : ":") + Minutes + ((Seconds < 10) ? ":0" : ":") + Seconds + ((Hours > 12) ? " PM" : " AM")
  10. TimerKey = setTimeout("ShowTime()",1000)
  11. }
  12.  
name of the file is showtime.js
please give me the instruction to use it

Newbie
 
Join Date: Feb 2008
Posts: 24
#2: Feb 7 '08

re: how to apply javascript without any event


Quote:

Originally Posted by agarwalsunitadhn

hi....
i want to display current date and time of client side in the page. i have the javascript code also bt i dnt know how to use it in the page. the code is

Expand|Select|Wrap|Line Numbers
  1. function ShowTime()
  2. {
  3. var TimerKey
  4. var now = new Date()
  5. var Hours = now.getHours()
  6. var Minutes = now.getMinutes()
  7. var Seconds = now.getSeconds()
  8. TimeDisplay.innerText = ((Hours > 12) ? Hours - 12 : Hours) + ((Minutes < 10) ? ":0" : ":") + Minutes + ((Seconds < 10) ? ":0" : ":") + Seconds + ((Hours > 12) ? " PM" : " AM")
  9. window.status = ((Hours > 12) ? Hours - 12 : Hours) + ((Minutes < 10) ? ":0" : ":") + Minutes + ((Seconds < 10) ? ":0" : ":") + Seconds + ((Hours > 12) ? " PM" : " AM")
  10. TimerKey = setTimeout("ShowTime()",1000)
  11. }
  12.  
name of the file is showtime.js
please give me the instruction to use it

In your HTML HEAD section.

include the JS file using

<script src="filename.js"></script>

Also somewhere you should define what "TimerDisplay" is...

Let us say there is a div as shown ..

<div id="timer"></div>

within head ....
<script>
startShowingTime()
{
TimerDisplay = document.getElementById("timer");
ShowTime();
}
</script>

Again, I am not a scripting expert. But you can use something like setTimeOut() event to periodically invoke ShowTime()
Reply