定時(shí)器
定時(shí)器的基本用法
setTimeout 只執(zhí)行一次的定時(shí)器
clearTimeout 關(guān)閉只執(zhí)行一次的定時(shí)器
setInterval 反復(fù)執(zhí)行的定時(shí)器
clearInterval 關(guān)閉反復(fù)執(zhí)行的定時(shí)器
單次定時(shí)器
var timer = setTimeout(function(){
alert('hello!');
}, 3000);
清除單次定時(shí)器
clearTimeout(timer);
反復(fù)循環(huán)定時(shí)器
var timer2 = setInterval(function(){
alert('hi~~~');
}, 2000);
清除反復(fù)循環(huán)定時(shí)器
clearInterval(timer2);
定時(shí)器彈框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>定時(shí)器彈框</title>
<style type="text/css">
.pop{
width: 400px;
height: 300px;
background-color: #fff;
border: 1px solid #000;
/*固定定位*/
position: fixed;
/*左上角位于頁面中心*/
left: 50%;
top: 50%;
/*讓div向左偏移半個(gè)寬度愕鼓、向上偏移半個(gè)高度愿题,使div位于頁面中心*/
margin-left: -200px;
margin-top: -150px;
/*彈窗在最上面*/
z-index: 9999;
}
/*遮罩樣式*/
.mask{
position: fixed;
width: 100%;
height: 100%;
background-color: #000;
left: 0;
top: 0;
/*設(shè)置透明度30%*/
opacity: 0.3;
filter: alpha(opacity=30);/*兼容IE6堪旧、7涩禀、8*/
/*遮罩在彈窗的下面,在網(wǎng)頁所有內(nèi)容的上面*/
z-index: 9990;
}
.pop_con{
display: none;/*默認(rèn)不顯示咆繁,用定時(shí)器顯示*/
}
</style>
<script type="text/javascript">
/*
setTimeout 只執(zhí)行一次的定時(shí)器
clearTimeout 關(guān)閉只執(zhí)行一次的定時(shí)器
setInterval 反復(fù)執(zhí)行的定時(shí)器
clearInterval 關(guān)閉反復(fù)執(zhí)行的定時(shí)器
*/
window.onload = function(){
var oPop = document.getElementById('pop');
var oShut = document.getElementById('shutOff');
/*setTimeout(showPop, 3000);//開啟定時(shí)器讳推,3秒后調(diào)用函數(shù)showPop()彈框
function showPop(){
oPop.style.display = 'block';//顯示彈框和遮罩
}*/
//開啟定時(shí)器的簡寫方式:調(diào)用匿名函數(shù)
setTimeout(function(){
oPop.style.display = 'block';
}, 3000);
oShut.onclick = function(){
oPop.style.display = 'none';//關(guān)閉彈框和遮罩
}
}
</script>
</head>
<body>
<h1>首頁標(biāo)題</h1>
<p>頁面內(nèi)容</p>
<a >百度網(wǎng)</a>
<div class="pop_con" id="pop">
<div class="pop">
<h3>提示信息!</h3>
<a href="#" id="shutOff">關(guān)閉</a>
</div>
<div class="mask"></div>
</div>
</body>
</html>
定時(shí)器動(dòng)畫
<style type="text/css">
.box{
width: 100px;
height: 100px;
background-color: gold;
position: fixed;
left: 20px;
top: 20px;
}
</style>
<script type="text/javascript">
window.onload = function(){
var oBox = document.getElementById('box');
var left = 20;
反復(fù)循環(huán)定時(shí)器玩般,每30毫秒修改一次盒子的left值
var timer = setInterval(function(){
left += 2;
oBox.style.left = left + 'px';
當(dāng)left值大于700時(shí)停止動(dòng)畫(清除定時(shí)器)
if(left > 700){
clearInterval(timer);
}
},30);
}
</script>
</head>
<body>
<div class="box" id="box"></div>
</body>
</html>
定時(shí)器制作時(shí)鐘
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>時(shí)鐘</title>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload = function(){
var oBox = document.getElementById('box');
function timeGo(){
var now = new Date();
// alert(now);//彈出美式時(shí)間:Wed Jun 20 2018 15:27:13 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
var year = now.getFullYear();//2018年
var month = now.getMonth() + 1;//6月彈出5//范圍0-11
var date = now.getDate();//20號(hào)
var week = now.getDay();//3//星期幾银觅,西半球時(shí)間,范圍0-6坏为,星期日為一周的第一天究驴,為0
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
// alert(hour + ":" + minute + ":" + second);//15:33:9
oBox.innerHTML = '當(dāng)前時(shí)間是:' + year + '年' + toDouble(month) + '月' + toDouble(date) + '日 ' + toWeek(week) + ' ' + toDouble(hour) + ":" + toDouble(minute) + ":" + toDouble(second);
}
timeGo();
setInterval(timeGo, 1000);
}
//此函數(shù)將星期的數(shù)字轉(zhuǎn)為漢字表示
function toWeek(num){
switch(num){
case 0:
return '星期天';
break;
case 1:
return '星期一';
break;
case 2:
return '星期二';
break;
case 3:
return '星期三';
break;
case 4:
return '星期四';
break;
case 5:
return '星期五';
break;
case 6:
return '星期六';
break;
}
}
//此函數(shù)將不足兩位的數(shù)字前面補(bǔ)0
function toDouble(num){
if(num < 10){
return '0' + num;
}else{
return num;
}
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
定時(shí)器倒計(jì)時(shí)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>倒計(jì)時(shí)</title>
<script type="text/javascript">
window.onload = function(){
//活動(dòng)第二天要將頁面下線,直接跳轉(zhuǎn)到其它頁面匀伏,不會(huì)走后面的代碼了
// window.location.;
var oDiv = document.getElementById('div1');
function timeLeft(){
//實(shí)際開發(fā)中此時(shí)間從服務(wù)器獲取纳胧,避免客戶端調(diào)整時(shí)間
var now = new Date();
var future = new Date(2018,5,20,16,30,20);
/ alert(future - now);//彈出與當(dāng)前時(shí)間相差的毫秒數(shù):12469935436
var milli = parseInt((future - now)/1000);
活動(dòng)當(dāng)天頁面下線,避免倒計(jì)時(shí)到點(diǎn)后繼續(xù)計(jì)負(fù)時(shí)
/if(milli <= 0){
// /頁面跳轉(zhuǎn)帘撰,不執(zhí)行下面的代碼了
/ window.location.;
/ }
var day = parseInt(milli / 86400);
var hour = parseInt(milli % 86400 / 3600);
var minute = parseInt(((milli % 86400) % 3600) / 60);
var second = milli % 60;
oDiv.innerHTML = '距離2018年11月12日00時(shí)00分00秒還有' + day + '天' + toDouble(hour) + '時(shí)' + toDouble(minute) + '分' + toDouble(second) + '秒';
}
timeLeft();
setInterval(timeLeft, 1000);
}
function toDouble(num){
if(num < 10){
return '0' + num;
}else{
return num;
}
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>