要實(shí)現(xiàn)一個(gè)在線顯示時(shí)間倒計(jì)時(shí)秒表的功能,可以使用HTML、CSS和JavaScript。以下是一個(gè)簡單的示例:
- 創(chuàng)建一個(gè)HTML文件,添加以下內(nèi)容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>倒計(jì)時(shí)秒表</title>
<style>
/* 在這里添加樣式 */
</style>
</head>
<body>
<div id="countdown"></div>
<script src="countdown.js"></script>
</body>
</html>
- 接下來,創(chuàng)建一個(gè)名為
countdown.js
的JavaScript文件,添加以下內(nèi)容:
// 獲取倒計(jì)時(shí)元素
const countdownElement = document.getElementById('countdown');
// 設(shè)置倒計(jì)時(shí)時(shí)間(單位:秒)
const countdownTime = 5; // 例如:5秒
// 更新倒計(jì)時(shí)顯示
function updateCountdown() {
const minutes = Math.floor(countdownTime / 60);
const seconds = Math.floor((countdownTime % 60) / 1);
const formattedSeconds = String(seconds).padStart(2, '0');
countdownElement.textContent = `${minutes}:${formattedSeconds}`;
}
// 開始倒計(jì)時(shí)
function startCountdown() {
setInterval(updateCountdown, 1000); // 每秒更新一次
}
// 停止倒計(jì)時(shí)
function stopCountdown() {
clearInterval(startCountdown); // 停止倒計(jì)時(shí)
}
// 初始化倒計(jì)時(shí)并開始計(jì)時(shí)
startCountdown();
- 在HTML文件中,將
<script src="countdown.js"></script>
添加到<body>
標(biāo)簽內(nèi)。運(yùn)行HTML文件,你應(yīng)該能看到一個(gè)顯示倒計(jì)時(shí)的頁面。點(diǎn)擊頁面上的“開始”按鈕,倒計(jì)時(shí)將開始;點(diǎn)擊“停止”按鈕,倒計(jì)時(shí)將停止。
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。