Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

toLocaleString()

This method returns the date in the form of a string, using the current locale’s conventions.

If you are trying to pass a date using toLocaleString, be aware that different locales assemble the string in different ways. Using methods such as getHours, getMinutes, and getSeconds will give more portable results. The following example demonstrates it:

var now = new Date()
var ar1 = now.toLocaleString().split(" ")
document.write("The current time is " + ar1[1])

The script’s output is:

The current time is 18:12:51

The general format of the converted string is:

MM/DD/YY HH:MM:SS

parse Methods

parse()

The parse method accepts a date string in the IETF standard and converts it to the number of milliseconds since January 1, 1970 00:00:00. The IETF standard date representation is:

DayAbb, date MonthAbb year HH:MM:SS TimeZoneAbb

An example for this standard is “Mon, 25 Dec 1995 13:30:00 GMT.” This method also understands the continental U.S. time zone abbreviations such as PST (Pacific Standard Time) and EST (Eastern Standard Time). However, time zones outside the United States (and their equivalent in Canada, for instance) do not have a standard abbreviation accepted by JavaScript. For such time zones the offset must be specified; that is, you must specify the difference in hours and minutes between the local time zone and Greenwich Mean Time. For example, in “Mon, 25 Dec 1995 13:30:00 GMT+0430,” GMT+0430 is shorthand for 4 hours, 30 minutes west of the Greenwich meridian. If you do not specify a time zone, the local time zone is assumed, according to the settings of the clock in the operating system. If your time zone is not set correctly, you should change it in the control panel, both on Macs and Windows-based machines. GMT is also known as Universal Coordinate Time, or UTC.

The parse method is a static one. It does not belong to a specific instance of the Date object, but to the object type itself. Therefore, it is always used as Date.parse(). Here is an example for this method:

var aDate = "Aug 27 1985"
var birthday = new Date()
birthday.setTime(Date.parse(aDate))

UTC()

The UTC() method takes a comma-delimited list and returns the number of milliseconds since January 1, 1970 00:00:00, Greenwich Mean Time (GMT, UTC). This is also a static method, so it is called along with the general Date object. You cannot use this method to refer to a specific date in the local time zone, because it constantly refers to the Universal Coordinate Time (GMT, UTC). For example, the following statement creates a date object using GMT instead of local time, as it would if the method was not used:

gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0))

The general syntax of the method is:

Date.UTC(year, month, day [, hrs] [, min] [, sec])

All attributes should be specified as integers.

Time-Related Methods of Other Objects

setTimeout()

The setTimeout() method evaluates an expression after a specified number of milliseconds have elapsed. Its general syntax is:

timeoutID = setTimeout(expression, msec)

timeoutID is an identifier used to identify the current timeout.

expression is a string expression or property of an existing object. It is normally a simple statement that is to be executed after the specified time has ticked off.

msec is a numeric value, a numeric string, or a property of an existing object in millisecond units.

The setTimeout() method evaluates an expression after a specified amount of time. Take a look at the following example:

<HTML>
<HEAD>
<TITLE>setTimeout() method</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function displayAlert() {
  alert("5 seconds have elapsed since the button was clicked.")
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
Click the button on the left for a reminder in 5 seconds;
click the button on the right to cancel the reminder before
it is displayed.
<P>
<INPUT TYPE="button" VALUE="5-second reminder"
 NAME="remind_button"
 onClick="timerID = setTimeout('displayAlert()',5000)">
</FORM>
</BODY>
</HTML>

When you click the button, the event handler’s script sets a timeout. The timeout specifies that after 5,000 milliseconds, or five seconds, the function displayAlert() is called. Therefore, five seconds after you click the button an alert box is displayed.


Note that the identifier for the timeout does not have to be timerID. You can use any valid identifier, even your name if you wish.


This method does not repeatedly execute the specified statement. That is, it does not execute it every five seconds, for example. When the time limit specified has ticked down, the statement is executed and the timeout does not exist anymore. SetTimeout() is a method of the window or frame object, depending on the basic structure of the HTML document it is used in.

It is common to use the setTimeout() method for creating a pause between two consecutive calls to a user-defined recursive function.

The setTimeout() method is probably one of the most difficult to use, because it requires a deep understanding of the language. Take a look at the following script:

function alertNumbers(num) {
  if (num > 10)
return
 alert(num)
   val = ++num
 timerID = setTimeout("alertNumbers(val)", 3000)
}

alertNumbers(0)

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us