The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). It will continue calling the function until clearInterval() is called or the window is closed.
//Fri Jan 02 1970 00:00:00 var d1 = new Date(86400000);
//Fri Jan 02 2015 10:42:00 var d2 = new Date("January 2, 2015 10:42:00");
//Sat Jun 11 1988 11:42:00 var d3 = new Date(88,5,11,11,42,0,0);
TIP:JavaScript counts months from 0 to 11. January is 0, and December is 11. Date objects are static, rather than dynamic. The computer time is ticking, but date objects don't change, once created.
When a Date object is created, a number of methods make it possible to perform operations on it.
當日期物件被新增,很多方法就可以在它上面執行運作。
For example:
範例:
var d = new Date(); var hours = d.getHours(); //hours is equal to the current hour
Let's create a program that prints the current time to the browser once every second.
我們來新增一個程式,讓瀏覽器每秒都印出當下的時間。
function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.body.innerHTML = hours+":"+mins+":"+secs; } setInterval(printTime, 1000);
We declared a function printTime(), which gets the current time from the date object, and prints it to the screen. We then called the function once every second, using the setInterval method.
TIP:The innerHTML property sets or returns the HTML content of an element. In our case, we are changing the HTML content of our document's body. This overwrites the content every second, instead of printing it repeatedly to the screen.
When you open any webpage in a browser, the HTML of the page is loaded and rendered visually on the screen. To accomplish that, the browser builds the Document Object Model of that page, which is an object oriented model of its logical structure. The DOM of an HTML document can be represented as a nested set of boxes:
The DOM represents a document as a tree structure. HTML elements become interrelated nodes in the tree. All those nodes in the tree have some kind of relations among each other. Nodes can have child nodes. Nodes on the same tree level are called siblings. For example, consider the following structure:
For the example above: <html> has two children (<head>, <body>); <head> has one child (<title>) and one parent (<html>); <title> has one parent (<head>) and no children; <body> has two children (<h1> and <a>) and one parent (<html>);
TIP:It is important to understand the relationships between elements in an HTML document in order to be able to manipulate them with JavaScript.
提示:了解HTML文件中元素的關係是很重要的,為了能在JavaScript中操作他們。
-----
The document Object
文件物件
There is a predefined document object in JavaScript, which can be used to access all elements on the DOM. In other words, the document object is the owner (or root) of all objects in your webpage. So, if you want to access objects in an HTML page, you always start with accessing the document object. For example:
TIP:The innerHTML property can be used on almost all HTML elements to change its content.
提示:innerHTML屬性可以用在幾乎全部的HTML元素去改變它的內容。
-----
翻譯小心得| 在REPLIT測試new Date沒有抓到正確的當地時間(UTC+8),查了一下資料,還沒得到解答。開始到DOM的篇章,這是之前接觸時就覺得特別有趣而且實用的部分,期待下周更深入的內容。 Finally, the article above is from SOLOLEARN . I translated it just for practice, personal use only. If that is inappropriate, please contact me.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.