JavaScript 简单 计时器运用,实现电子时钟自动走动的效果,按钮控制启止? 20
2个回答
展开全部
<html><head><script type="text/javascript">function startTime(){var today=new Date()var h=today.getHours()var m=today.getMinutes()var s=today.getSeconds()// add a zero in front of numbers<10m=checkTime(m)s=checkTime(s)document.getElementById('txt').innerHTML=h+":"+m+":"+st=setTimeout('startTime()',500)} function checkTime(i){if (i<10) {i="0" + i} return i}</script></head> <body onload="startTime()"><div id="txt"></div></body></html>
展开全部
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>setInterval stopwatch</title>
<style>
p {
font-family: sans-serif;
}
</style>
</head>
<body>
<p class="clock"></p >
<p>
<button class="start">Start</button>
<button class="stop">Stop</button>
<button class="reset">Reset</button>
</p >
<script>
let secondCount = 0;
let stopWatch;
const displayPara = document.querySelector('.clock');
function displayCount() {
let hours = Math.floor(secondCount/3600);
let minutes = Math.floor((secondCount % 3600)/60);
let seconds = Math.floor(secondCount % 60)
let displayHours = (hours < 10) ? '0' + hours : hours;
let displayMinutes = (minutes < 10) ? '0' + minutes : minutes;
let displaySeconds = (seconds < 10) ? '0' + seconds : seconds;
displayPara.textContent = displayHours + ':' + displayMinutes + ':' + displaySeconds;
secondCount++;
}
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>setInterval stopwatch</title>
<style>
p {
font-family: sans-serif;
}
</style>
</head>
<body>
<p class="clock"></p >
<p>
<button class="start">Start</button>
<button class="stop">Stop</button>
<button class="reset">Reset</button>
</p >
<script>
let secondCount = 0;
let stopWatch;
const displayPara = document.querySelector('.clock');
function displayCount() {
let hours = Math.floor(secondCount/3600);
let minutes = Math.floor((secondCount % 3600)/60);
let seconds = Math.floor(secondCount % 60)
let displayHours = (hours < 10) ? '0' + hours : hours;
let displayMinutes = (minutes < 10) ? '0' + minutes : minutes;
let displaySeconds = (seconds < 10) ? '0' + seconds : seconds;
displayPara.textContent = displayHours + ':' + displayMinutes + ':' + displaySeconds;
secondCount++;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询