<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
padding: 0;
}
#box{
width: 400px;
height: 300px;
margin: 100px auto;
background: lightblue;
}
#header {
width: 100px;
height: 50px;
font-size: 30px;
font-weight: bold;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="box">
<div id="header">闹钟</div>
<input id="time" type="number">分钟
<button id="setTime">设置</button>
<div>
<h2 id="second"></h2>倒计时
</div>
<audio id="music" src="game_music.mp3"></audio>
</div>
</body>
</html>
<script>
const time = document.getElementById('time')
const setTime = document.getElementById('setTime')
const second = document.getElementById('second')
const music = document.getElementById('music')
function center() {
let i = time.value * 60
setInterval(function () {
if (i > 0) {
i--;
}
second.innerHTML = i
if (i == 0) {
music.play()
}
}, 1000)
}
setTime.onclick = function () {
center()
}
</script>