index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>xzm </title>
</head>
<body>
<button id="timer" onclick="ATimer('timer')">获取验证码</button>
</body>
<script type="text/javascript" src="js/计时器.js"></script>
</html>
计时器.js
var times=0
function Interval(id){
var oo=setInterval(function(){
if (times-1<=0){
Close(oo)
}
times--
if (times<=0){
var id1 = document.getElementById(id);
id1.textContent="重新获取验证码"
}else {
var id1 = document.getElementById(id);
id1.textContent=times
}
},200)
}
function Close(id){
clearInterval(id)
}
function ATimer(id) {
if (times <= 0) {
times = 60
Interval(id);
}else {
alert("请在"+times+"秒后再试")
}
}
最后别忘了,修改js的路径与本地路径匹配!!!