-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path倒計時按鈕.html
70 lines (65 loc) · 1.74 KB
/
倒計時按鈕.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#box{
width: 200px;
height: 200px;
margin: 200px auto;
}
</style>
</head>
<body>
<script>
var clock = '';
var nums = 10;
var btn;
function sendCode(thisBtn){
btn = thisBtn;
btn.disabled = true; //将按钮置为不可点击
btn.value = nums+'秒后可重新获取';
clock = setInterval(doLoop, 1000); //一秒执行一次
}
function doLoop() {
nums--;
if(nums > 0){
btn.value = nums+'秒后可重新获取';
}else{
clearInterval(clock); //清除js定时器
btn.disabled = false;
btn.value = '点击发送验证码';
nums = 10; //重置时间
}
}
</script>
<div id="box">
<button id="btn">click</button>
<input type="button" style="height:32px;width:120px;" value="点击发送验证码" onclick="sendCode(this)" />
</div>
<script>
window.onload=()=>{
let btn=document.getElementById('btn')
function djs() {
let time=6
console.log()
let oldVal=this.innerText
let that=this
let id=setInterval(function () {
if(time>0){
time--
that.innerText=time
that.disabled=true
}else {
window.clearInterval(id);
that.innerText=oldVal
that.disabled=false
}
},1000)
}
btn.addEventListener('click',djs)
}
</script>
</body>
</html>