Professional Documents
Culture Documents
The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.
The ID value returned by setInterval() is used as the parameter for the clearInterval() method.
Tip: To execute a function only once, after a specified number of milliseconds, use the setTimeout() method.
Syntax
setInterval(function, milliseconds, param1, param2, ...)
Window setInterval() Method
Example Example
You can also refer to a "named" function; Alert Display the current time (the setInterval() method
"Hello" every 3 seconds (3000 milliseconds): will execute the function once every 1 second, just
like a digital watch):
var myVar;
var myVar = setInterval(myTimer, 1000);
function myFunction() {
myVar = setInterval(alertFunc, 3000); function myTimer() {
} var d = new Date();
var t = d.toLocaleTimeString();
function alertFunc() {
alert("Hello!"); document.getElementById("demo").innerHTML
} = t;
}
What You Will Learn
In the next chapters of this tutorial you will learn:
When a web page is loaded, the browser creates a Document Object Model of the page.
● How to change the content of HTML elements
The HowDOM
● HTML to change
modelthe style (CSS) as
is constructed of HTML
a tree elements
of Objects:
● How to react to HTML DOM events
● How to add and delete HTML elements
In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML elements.
The HTML DOM
A property is a value that you can get or set (like changing the content of an HTML element).
Example
The following example changes the content (the innerHTML) of the <p> element with id="demo":
The HTML DOM Document Object
The document object represents your web page.
If you want to access any element in an HTML page, you always start with accessing the document object.
Below are some examples of how you can use the document object to access and manipulate HTML.
Method Description
If you want to find all HTML elements that match a specified CSS selector (id, class names, types,
attributes, values of attributes, etc), use the querySelectorAll() method.
Changing HTML Elements
Property Description
Method Description
namedItem() Returns the element with the specified ID, or name, in an HTMLCollection
The Browser There are no official standards for
the Browser Object Model (BOM).
Object Model
(BOM) Since modern browsers have
implemented (almost) the same
methods and properties for
JavaScript interactivity, it is often
referred to, as methods and
properties of the BOM.
The Window Object
The window object is supported by all browsers. It represents the browser's window.
All global JavaScript objects, functions, and variables automatically become members of the window object.
Even the document object (of the HTML DOM) is a property of the window object:
window.document.getElementById("header");
document.getElementById("header");
The Window Object
Depth
The screen.colorDepth property returns the
number of bits used to display one color. Example
All modern computers use 24 bit or 32 bit hardware
for color resolution: Display the color depth of the screen in bits:
Syntax
window.alert("sometext");
Example
alert("I am an alert box!");
The Window Object -JavaScript Popup Boxes
Confirm Box The window.confirm() method can be written
without the window prefix.
When a confirm box pops up, the user will have to if (confirm("Press a button!")) {
click either "OK" or "Cancel" to proceed.
txt = "You pressed OK!";
If the user clicks "OK", the box returns true. If the
user clicks "Cancel", the box returns false. } else {
Syntax
window.prompt("sometext","defaultText");
The Window Object -JavaScript Popup Boxes
Line Breaks
To display line breaks inside a popup box, use a back-slash followed by the character n.
Example
Window Navigator
The window.navigator object can be written
without the window prefix.
Some examples:
● navigator.appName
● navigator.appCodeName
● navigator.platform
The Window Object
Window Location
The window.location object can be written without the window prefix.
Some examples:
Window History
The window.history object can be written without the window prefix.
To protect the privacy of the users, there are limitations to how JavaScript can access this object.
Some methods:
●
JavaScript Cookies
● When a user visits a web page, his/her name can
Cookies let you store user information in web
be stored in a cookie.
pages.
● Next time the user visits the page, the cookie
"remembers" his/her name.
Cookies were invented to solve the problem "how None of the examples below will work if your browser has
to remember information about the user": local cookies support turned off.
JavaScript Cookies
With JavaScript, a cookie can be created like this: document.cookie = "username=John Doe;
expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/"
;