"js learning" Timer

Source: Internet
Author: User

I. The function of the timer

(1) Turn on the timer

SetInterval Spacer Type

SetTimeout Delay Type

The difference between the two types of timers

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">

<meta http-equiv= "content-type" content= "text/html; Charset=utf-8 "/>
<title> Timers </title>
<script>
Function Show ()
{
Alert (' a ');
};
SetInterval (show,1000);//perform a show function every 1000 milliseconds
SetTimeout (show,1000);//execute Show function after 1000 milliseconds delay
</script>



<body>
</body>

(2) Stop Timer

Clearinterval

Cleartimeout

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">

<meta http-equiv= "content-type" content= "text/html; Charset=utf-8 "/>
<title> Timer opening and closing </title>
<script>
Window.onload = function ()
{
var Obtn1=document.getelementbyid (' btn1 ');
var Obtn2=document.getelementbyid (' btn2 ');
var timer=null;

Obtn1.onclick = function ()
{
Timer=setinterval (function ()
{
Alert (' a ');
},1000);
};

Obtn2.onclick = function ()
{
Clearinterval (timer);
};
};
</script>


<body>
<input id= "btn1" type= "button" value= "on"/>
<input id= "btn2" type= "button" value= "off"/>
</body>


second, the digital clock

(1) Effect Idea

(2) Get system time

Data Object

gerhours, getminutes, getseconds

(3) Display System time

String connection

Vacancy 0

(4) Set Picture path

Charat method

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">

<meta http-equiv= "content-type" content= "text/html; Charset=utf-8 "/>
<title> Digital clock (compatibility mode) </title>
<script>
But not compatible IE7
function Todou (n)
{
If (n<10)
{
Return ' 0 ' +n;//first bit plus 0, guaranteed to take out the time number is always a 2-digit string, such as 01
}
Else
{
Return ' +n;//why do you want to add an empty string ', so that the returned 2-bit number is returned as a string
}
};

Window.onload = function ()
{
var aimg=document.getelementsbytagname (' img ');
var str= ' 013221 ';//this time is written to death
Turn on the timer and update the picture every Second.
function Tick ()
{
var odate=new Date ();

var Str=todou (odate.gethours ()) +todou (odate.getminutes ()) +todou (odate.getseconds ());

For (var I=0;i<aimg.length;i++)
{
Aimg[i].src= ' img/' +str.charat (i) + '. png ';//use charAt (i) to keep the various browsers compatible!
}
};
SetInterval (tick,1000);
Tick ();//the tick () function is executed in the onload first, so that there is no instantaneous delay of 0 when the page is just opened
};
</script>


<body style= "background:black; color:white; font-size:50px; " >


:


:


</body>


third, Other methods of the Date object

Year getFullYear

Month GetMonth ()

Day GetDate ()

Week Getday ()

four, Delay Prompt Box

(1) Effect Demonstration

(2) the original method

Move in display, move out of hidden

(3) Move out delay hide

After you move into the following div, you still hide the

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">

<meta http-equiv= "content-type" content= "text/html; Charset=utf-8 "/>
<title> Delay Prompt Box </title>
<style>
div{
margin:10px;
float:left;
}
#div1 {
width:50px;
height:50px;
background:red;
}
#div2 {
width:250px;
height:180px;
Background: #CCC;
display:none;
}
</style>
<script>
Window.onload = function ()
{
var Odiv1=document.getelementbyid (' Div1 ');
var Odiv2=document.getelementbyid (' Div2 ');
var timer=null;

Odiv1.onmouseover=function ()
{
Cleartimeout (timer);//if You do not clear Div2 timeout this time bomb, when the mouse moved from div2 into the div1, div2 will disappear
Odiv2.style.display= ' Block ';
};
Odiv1.onmouseout=function ()
{
Timer=settimeout (function ()
{
Odiv2.style.display= ' None ';
},500);
};
When the mouse moves to the Div2, turn off the timer first
Odiv2.onmouseover=function ()
{
Cleartimeout (timer);
};

Odiv2.onmouseout=function ()
{
Timer=settimeout (function ()
{
Odiv2.style.display= ' None ';
},500);

};
};

</script>


<body>
<div id= "div1" >

</div>
<div id= "div2" >

</div>
</body>


(4) Simplified Code

Merge two identical mouseover and mouseout

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">

<meta http-equiv= "content-type" content= "text/html; Charset=utf-8 "/>
<title> Delay Prompt Box Merge code </title>
<style>
div{
margin:10px;
float:left;
}
#div1 {
width:50px;
height:50px;
background:red;
}
#div2 {
width:250px;
height:180px;
Background: #CCC;
display:none;
}
</style>
<script>
Window.onload = function ()
{
var Odiv1=document.getelementbyid (' Div1 ');
var Odiv2=document.getelementbyid (' Div2 ');
var timer=null;

Odiv2.onmouseover=odiv1.onmouseover=function ()
{
Cleartimeout (timer);//if You do not clear Div2 timeout this time bomb, when the mouse moved from div2 into the div1, div2 will disappear
Odiv2.style.display= ' Block ';
};
Odiv2.onmouseout=odiv1.onmouseout=function ()
{
Timer=settimeout (function ()
{
Odiv2.style.display= ' None ';
},500);
};
};

</script>


<body>
<div id= "div1" >

</div>
<div id= "div2" >

</div>
</body>


"js learning" Timer

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.