主要用到setInterval和clearInterval方法和一个计时变量。
效果
代码
<!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>BOM</title>
<style>
*{
margin: 10px;
}
</style>
</head>
<body>
<input type="text"><button>发送</button>
</body>
<script>
var btn = document.querySelector('button');
btn.addEventListener('click',function(){
btn.disabled = true;
var i = 6;
var timer = setInterval(function(){
i --;
btn.innerHTML = "再过"+i+"s才能发送";
if(i==0){
clearInterval(timer);
btn.disabled = false;
btn.innerHTML = "发送";
}
},1000);
})
</script>
</html>