The use and difference between window and document object, SetInterval and settimeout timers in JavaScript

Source: Internet
Author: User

First, write in front

I front-end rookie A, learning the front end soon, there are many concepts in the learning process, the definition is easy to confuse in use, in this to me just as soon as the front door of the children's shoes to summarize. Today, I would like to share with you the use and difference between window and document object, SetInterval and settimeout timer in JS. Speak the wrong place, please correct me, but also hope that the elder, Daniel more advice!

Second, the use and difference between the Window object and the Document object

Window is a global object, and document is a property of the Window object and it is also an object. :

The Document object refers to the page of the object, window is global, the Window object is the core of the BOM object, you do in the browser all the JS is in the scope of this object.

1. Properties, methods, events for Window objects

See here for properties, methods, events, and how to use the Window object.

2, the Document object commonly used properties, methods, events

(1) Properties:

Document.title//Setting document title Title tag

Document. URL//Set URL to open another page in the same window

Document.cookie//Set up and read out cookies

Document.charset//Set character sets

(2) Common methods

document.write ()//write content to page

Document.createelement (tag)//Create HTML tag object

document.getElementById (ID)//Get the object with the specified ID value

Document.getelementsbyname (name)//Gets the object with the specified name value

Document.body.appendChild (Element)//Inserts an element node into the body as the last child element

document.getElementsByTagName (tag)//Gets the specified element node

(3) Common object events

document.body.onclick= func ()//mouse pointer Click object is triggered

Document.body.onmouseover = func ()//trigger when mouse pointer moves to object

Document.body.onmouseout = func ()//trigger when mouse pointer moves out of object

three, setinterval and settimeout timer usage and difference

SetInterval () is an intermittent call that executes code once every specified time, and setTimeout () executes once (only once) within a specified time, as shown in the following code:

<!DOCTYPE HTML><HTMLLang= "en">    <Head>        <title></title>        <MetaCharSet= "Utf-8">    </Head>    <Scripttype= "Text/javascript">window.onload= function(){            varOBTN1=document.getElementById ('button1'); varoBtn2=document.getElementById ('Button2'); Obtn1.onclick= function() {setinterval (function() {alert ('repeated execution'); },  +);            }; Obtn2.onclick= function() {setTimeout (function() {alert ('Execute only once'); },  +);        };    }; </Script>    <Body>        <inputID= "Button1"type= "button"value= "Repeated execution" />        <inputID= "Button2"type= "button"value= "Execute only once" />    </Body></HTML>

As the code adds a mouse click event to the "Repeat Execution" button, a setinterval () timer is defined within the function of the Click event, and a "repeat" prompt box pops up each second when the "Repeat Execution" button is clicked (no longer clicked); The button also adds a mouse click event that defines a settimeout () timer within the function of the Click event, and when the "execute Once" button is clicked, only one second after the "execute Once" prompt pops up only when you click again.

Iv. Summary

These two sets of concepts in peacetime learning is still more common, know their respective usage, use is more handy. I am a novice, if there is a wrong place in the text, please correct me! To the point, rest good, can study good, everybody good night!

This article published in my Blog Park Changjianqiu, if you want to reprint this article, please specify the source: http://www.cnblogs.com/changjianqiu/

The use and difference between window and document object, SetInterval and settimeout timers in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.