計時器,時鐘,秒錶,倒計時
阿新 • • 發佈:2021-01-09
技術標籤:資料型別javascript
定時器
在js中定時器有兩種
1、setInterval()
格式:var 變數名 = setInterval(“執行的語句”,每隔多久執行一次);
【注意】可以寫執行的程式碼,也可以直接傳入函式。
返回值:啟動定時器時,系統分配的編號
2、setTimeout()
只在指定時間後執行一次。
關閉:clearTimeout();
【注意】 clearTimeout函式需要一個引數:定時器的編號。
關閉計時器
關閉定時器的方式:
1.關閉頁面。
2.clearInterval();
【注意】 clearInterval函式需要一個引數:定時器的編號。
小時鐘
function showTime(d) {
// 獲取年
var year = d.getFullYear();
// 獲取月
var month = d.getMonth()+1;
// 獲取日
var date = d.getDate();
// 小時數
var hours = d.getHours();
// 獲取分鐘數
var m = d.getMinutes();
// 獲取秒數
var seconds = d.getSeconds ();
// 獲取星期幾
var day = d.getDay();
var str = year + "年" + doubleNum(month) +"月"+doubleNum(date)+"日 星期"+num2Chinese(day)+" "+hours+":"+m+":"+seconds;
return str;
}
// 數字轉中文
function num2Chinese(num) {
var arr = ["日" ,"一","二","三","四","五","六"];
return arr[num];
}
// 給小於10的前面補0
function doubleNum(n) {
if(n<10){
return "0"+n;
}else{
return n
}
}
秒錶
html程式碼
<div class="box">
<div id="showTime">
<span>00</span>
<span>:</span>
<span>00</span>
<span>:</span>
<span>00</span>
</div>
<div class="bnt">
<button id="resetBtn">復位</button>
<button id="startBtn">開始</button>
</div>
</div>
css程式碼
* {
margin: 0;
padding: 0;
}
.box {
margin: 10px auto;
width: 400px;
}
span {
font-size: 60px;
}
button {
width: 100px;
height: 100px;
border-radius: 50px;
font-size: 24px;
margin: 0 48px;
border: 0;
outline: none;
cursor: pointer;
}
#showTime {
text-align: center;
margin-bottom: 20px;
}
JavaScript程式碼
window.onload = function() {
var min = 0;
var s = 0;
var ms = 0;
var time1;
var spans = document.getElementsByTagName('span');
// 復位
$('button')[0].onclick = function() {
min = 0;
s = 0;
ms = 0;
spans[0].innerHTML = '00';
spans[2].innerHTML = '00';
spans[4].innerHTML = '00';
$('button')[1].innerHTML = '開始';
clearInterval(time1);
}
//開始
$('button')[1].onclick = function() {
if ($('button')[1].innerHTML == '開始') {
$('button')[1].innerHTML = '停止';
clearInterval(time1);
time1 = setInterval(show, 10);
} else {
$('button')[1].innerHTML = '開始';
clearInterval(time1);
}
}
function show() {
ms++;
if (ms == 100) {
ms = 0;
s++;
}
if (s == 60) {
s = 0;
min++;
}
var msStr = ms;
if (ms < 10) {
msStr = "0" + ms
}
var sStr = s;
if (s < 10) {
sStr = "0" + s;
}
var minStr = min;
if (min < 10) {
minStr = "0" + min;
}
spans[0].innerHTML = minStr;
spans[2].innerHTML = sStr;
spans[4].innerHTML = msStr;
}
function $(str) {
return document.getElementsByTagName(str);
}
}
倒計時
html程式碼
<div class="box">
<p class="round">12:00 場</p>
<img src="data:img/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAA1CAYAAAAklDnhAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
bWFnZVJlYWR5ccllPAAAAyNpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdp
bj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6
eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDE0IDc5LjE1
MTQ4MSwgMjAxMy8wMy8xMy0xMjowOToxNSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJo
dHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlw
dGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAv
IiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6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MkyZRs13wpgIRoiJKy7dsBWh1L
bN5Vt3JFQrQ2XMYeGKBk5yvSI7aRQ4k0eA4uZZlVPM47lDgTZxd6lKb4AcqkLuKSZiIdVGsiVFNc
xDV9yT1Orekjoe+MDw4pHqwmaJewit66Ql0QaX+DOTaZe4DitAFZxQaaEynKnJ6KCDeaQAtj3A7V
kBtXIpwT3iFV06zqquaKPKEtKIsM6Mo/StoWiQeMvSCJjO4+54hEwTCjuTk0STgckUm6QxTY3LgU
Kc+/jyFrblyKCIkKbnPjSkT8HDGV5saVyBAtVHZz40Ik7HNTRtrcuBARF2tQbW5ciCR0mhvbIs2E
cnNjWySh29zYFGnIK25KzY1Nkdziptzc2BKJUu7Qbm5sicQom2o3NzZEKqiu/DZpbmyIjIAHNBMZ
7x4iTALjhcgVQSIl3v87w5vePcY/AQYAFYR6skFSqBUAAAAASUVORK5CYII=" alt="小米閃購">
<p class="desc">距離結束還有</p>
<p class="countdown clearfix">
<span>00</span>
<i>:</i>
<span>00</span>
<i>:</i>
<span>00</span>
</p>
</div>
css程式碼
.box {
width: 234px;
height: 340px;
box-sizing: border-box;
margin: 0 auto;
border-top: 1px solid #e53935;
background-color: #f1eded;
text-align: center;
}
.round {
font-size: 21px;
color: #ef3a3b;
padding-top: 15px;
}
img {
margin: 25px 0;
border: none;
}
.desc {
color: rgba(0, 0, 0, .54);
font-size: 15px;
}
.countdown {
width: 168px;
margin: 28px auto 0;
}
.clearfix::after {
content: "";
clear: both;
display: block;
}
.countdown span {
width: 46px;
height: 46px;
background: #605751;
color: #fff;
font-size: 24px;
line-height: 46px;
float: left;
}
.countdown i {
width: 15px;
float: left;
height: 46px;
line-height: 46px;
color: #605751;
font-size: 28px;
font-style: normal;
}
JavaScript程式碼
方法1
window.onload = function() {
var spans = document.getElementsByTagName('span');
var desc = document.getElementsByClassName('desc')[0];
var time;
time = setInterval(countdown, 1000);
function countdown() {
var dates = (new Date(2021, 0, 8, 9, 48, 0) - new Date()) / 1000;
var h = parseInt(dates / 3600);
var m = parseInt(dates % 3600 / 60);
var s = parseInt(dates % 60);
if (dates < 0) {
clearInterval(time);
h = 0;
m = 0;
s = 0;
desc.innerHTML = '本場已經結束';
}
spans[0].innerHTML = h < 10 ? '0' + h : h;
spans[1].innerHTML = (m < 10) ? "0" + m : m;
spans[2].innerHTML = (s < 10) ? "0" + s : s;
}
}
方法2
window.onload = function() {
var date = new Date("2021-01-08 9:36:10");
// var date = new Date(2021,0,8,12,00,00);
var nowDate = new Date();
// 差值:總的秒數
var dateDiff = parseInt((date.getTime()-nowDate.getTime())/1000);
// 小時數
var hours = parseInt(dateDiff/3600);
// 分鐘數
var min = parseInt(dateDiff/60)%60;
//秒數
var sec = dateDiff%60;
var timer,spans,desc = null;
window.onload = function() {
spans = document.getElementsByTagName("span");
desc = document.getElementsByClassName("desc")[0];
timer = setInterval(showTime,1000);
}
function showTime() {
sec--;
if (sec < 0) {
sec = 59;
min--;
}
if (min<0) {
min=59;
hours--;
}
// 計時結束
if (hours<0) {
hours = 0;
min=0;
sec = 0;
clearInterval(timer);
desc.innerHTML = "該場次已結束";
}
spans[0].innerHTML = hours<10?"0"+hours:hours;
spans[1].innerHTML = min<10?"0"+min:min;
spans[2].innerHTML = sec<10?"0"+sec:sec;
}
}