JavaScript实现 按钮不可过频繁使用
作者:互联网
主要用到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>
标签:function,disabled,JavaScript,clearInterval,innerHTML,频繁,按钮,var,btn 来源: https://blog.csdn.net/weixin_44997802/article/details/121228081