分析
- 点击按钮后禁用按钮(disabled=true)
- 点击按钮后内容发生变化,通过innerHTML修改
- 利用定时器修改秒数
- 时间为0即停止定时器并且重新设定时间,复原按钮状态
代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Document</title>
<style>
</style>
</head>
<body>
手机号码:<input type="number" placeholder="请输入手机号"/>
<button>发送</button>
<script>
var btn = document.querySelector('button');
var time = 5;
btn.addEventListener('click', function () {
btn.disabled = true;
timer = setInterval(function () {
if(time <= 0) {
//清除定时器并且复原按钮
clearInterval(timer);
btn.disabled = false;
btn.innerHTML = '发送';
time = 5;
} else{
btn.innerHTML = '还剩下'+ time +'秒';
time--;
}
}, 1000)
})
</script>
</body>
</html>