<!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>
</head>
<body>
<h1>10</h1>
<button>开始</button>
</body>
</html>
var h1 = document.querySelector('h1')
var btn = document.querySelector('button')
var timer
// 创建防抖函数
function antiShake(output,ms) {
return function() {
clearInterval(timer)
timer = setInterval(output,ms)
}
}
// 创建被定时器调用函数
function out() {
var start = h1.innerText
var end = 0
start--
h1.innerText = start
if(start == end) {
clearInterval(timer)
}
}
btn.onclick = antiShake(out,1000)