<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<style>
#num {
width: 3rem;
}
</style>
<body>
<button id="get_code">点此获取验证码</button>
<script src="./jquery-1.12.4.min.js"></script>
<script>
var time = 60;
function setTime() {
var beforeTime = getTime();
window.setTimeInterval = setInterval(function () {
time = time - (((getTime() - beforeTime) / 1000).toFixed(0));
if (time <= 0) {
$("#get_code").text('获取验证码');
clearInterval(setTimeInterval);
return time = 60;
} else {
$("#get_code").text(time + ' s');
beforeTime = getTime();
}
}, 1000);
}
$("#get_code").click(function () {
setTime();
})
//获取当前毫秒数
function getTime() {
return Date.now();
}
</script>
</body>
</html>
解决