01
Feb
10

Me trying to explain some JavaScript code?

We are learning JavaScript in my scripting class and was asked to try and explain some code. I am going to do my best, but dont take my word.

<SCRIPT>                                                                                                           -Opens the script tag

function showTime() {                                                                               calls this function “showTime”
var now = new Date();                                                                                  Variable called “now” to show the date

var hours = now.getHours();                                                                  Variable called “hours” to get the current hour

var minutes = now.getMinutes();                                                        Variable called “minutes” to get the current minutes

var seconds = now.getSeconds();                                                        Variable called “seconds” to get the current seconds

var timeStr = “” + ((hours > 12) ? hours – 12 : hours);                    Variable called “timeStr” is saying if the hours are greater than 12 than subtract then subract 12 from the total hours and put “:” after the displayed number              (ex:hours=18 so subtract 12 and display 6:)

timeStr += ((minutes < 10) ? “:0″ : “:”) + minutes;                           Variable called “timeStr” is saying that if minutes are less than 10 display a 0 in front of number and put a “:” before (ex: its 9 miutes, display :09)

timeStr += ((seconds < 10) ? “:0″ : “:”) + seconds;                           Variable “timeStr” is asking to do the same as with the minutes but only with the seconds

timeStr += (hours >= 12) ? ” P.M.” : ” A.M.”;                                      If hours are greater than 12 display :P M if its less than 12 display :AM

status = timeStr; Time is displayed in the Status Line

setTimeout(“showTime()”, 1000);                                               Closes Function “showTime”
}

</SCRIPT>                                                                                                     Closes Script tag

<BODY onLoad=”showTime()>                                                    When site loads show “showtime” (5:50:46:P.M.) in the body of the page

Advertisement

0 Responses to “Me trying to explain some JavaScript code?”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.