读秒设置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="box">计时开始</div>
<button onclick="start()">开始</button>
<script>
function djs(callback) {
var s = ''
var timer = null
var box = document.querySelector('#box')
var handle = function () {
if (!timer) {
s = 60
handle.init()
callback && callback()
timer = setInterval(handle.init, 1000)
}
}
handle.init = function () {
box.innerHTML = s
if (s == 0 || s < 0) {
box.innerHTML = '重新开始'
clearInterval(timer)
timer = null
return false
}
s--
}
handle.cancel = function () {
s = 0
handle.init()
clearInterval(timer)
timer = null
}
return handle
}
var callback = function () {
console.log('我执行了')
}
var start = djs(callback)
</script>
</body>
</html>