前端 JS基礎理論總結(七)
阿新 • • 發佈:2019-01-12
定時器用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>定時器的基本用法</title> <script type="text/javascript"> //單次定時器 var timer = setTimeout(function(){ alert('hello!'); }, 3000); //清除單次定時器 clearTimeout(timer); //反覆迴圈定時器 var timer2 = setInterval(function(){ alert('hi~~~'); }, 2000); //清除反覆迴圈定時器 clearInterval(timer2); </script> </head> <body> </body> </html>
定時器動畫
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>定時器動畫</title> <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; //反覆迴圈定時器,每30毫秒修改一次盒子的left值 var timer = setInterval(function(){ left += 2; oBox.style.left = left + 'px'; //當left值大於700時停止動畫(清除定時器) if(left > 700){ clearInterval(timer); } },30); } </script> </head> <body> <div class="box" id="box"></div> </body> </html>
時鐘
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>時鐘</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);//彈出美式時間:Wed Jun 20 2018 15:27:13 GMT+0800 (中國標準時間) var year = now.getFullYear();//2018年 var month = now.getMonth() + 1;//6月彈出5//範圍0-11 var date = now.getDate();//20號 var week = now.getDay();//3//星期幾,西半球時間,範圍0-6,星期日為一週的第一天,為0 var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); // alert(hour + ":" + minute + ":" + second);//15:33:9 oBox.innerHTML = '當前時間是:' + year + '年' + toDouble(month) + '月' + toDouble(date) + '日 ' + toWeek(week) + ' ' + toDouble(hour) + ":" + toDouble(minute) + ":" + toDouble(second); } timeGo(); setInterval(timeGo, 1000); } //此函式將星期的數字轉為漢字表示 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; } } //此函式將不足兩位的數字前面補0 function toDouble(num){ if(num < 10){ return '0' + num; }else{ return num; } } </script> </head> <body> <div id="box"></div> </body> </html>
倒計時
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>倒計時</title>
<script type="text/javascript">
window.onload = function(){
//活動第二天要將頁面下線,直接跳轉到其它頁面,不會走後面的程式碼了
// window.location.href = "http://www.baidu.com";
var oDiv = document.getElementById('div1');
function timeLeft(){
//實際開發中此時間從伺服器獲取,避免客戶端調整時間
var now = new Date();
var future = new Date(2018,5,20,16,30,20);
// alert(future - now);//彈出與當前時間相差的毫秒數:12469935436
var milli = parseInt((future - now)/1000);
//活動當天頁面下線,避免倒計時到點後繼續計負時
// if(milli <= 0){
// //頁面跳轉,不執行下面的程式碼了
// window.location.href = "http://www.baidu.com";
// }
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時00分00秒還有' + day + '天' + toDouble(hour) + '時' + 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>