setInterval 一個(gè)定時(shí)器搞定

<style>
button{
background: #45BCF9;
color: #fff;
padding: 4px 10px;
border: none;
outline: none;
cursor: pointer;
}
button:hover{
background: #00a8fe;
}
button.disabled{
background: #000;
cursor: auto;
}
button.disabled:hover{
background: #000;
}
</style>
<button type="button" onclick="fn()">獲取驗(yàn)證碼</button>
<script>
function fn(){
var oBtn = document.getElementsByTagName('button')[0];
var time = 60;
var timer = null;
oBtn.innerHTML = time + '秒后重新發(fā)送';
oBtn.setAttribute('disabled', 'disabled'); // 禁用按鈕
oBtn.setAttribute('class', 'disabled'); // 添加class 按鈕樣式變灰
timer = setInterval(function(){
// 定時(shí)器到底了 兄弟們回家啦
if(time == 1){
clearInterval(timer);
oBtn.innerHTML = '獲取驗(yàn)證碼';
oBtn.removeAttribute('disabled');
oBtn.removeAttribute('class');
}else{
time--;
oBtn.innerHTML = time + '秒后重新發(fā)送';
}
}, 1000)
}
</script>
本文題目:JS實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)功能-創(chuàng)新互聯(lián)
本文URL:http://www.chinadenli.net/article4/deipie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、ChatGPT、面包屑導(dǎo)航、域名注冊(cè)、網(wǎng)站營(yíng)銷、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容