您好,登錄后才能下訂單哦!
這篇文章主要介紹vue、react如何實(shí)現(xiàn)倒計時效果,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
Vue
方案一:倆個元素
HTML:
<div id="example"> <button @click="send"> <span v-if="sendMsgDisabled">{{time+'秒后獲取'}}</span> <span v-if="!sendMsgDisabled">send</span> </button> </div>
JS:
var vm = new Vue({ el: '#example', data() { return { time: 60, // 發(fā)送驗(yàn)證碼倒計時 sendMsgDisabled: false } }, methods: { send() { let me = this; me.sendMsgDisabled = true; let interval = window.setInterval(function() { if ((me.time--) <= 0) { me.time = 60; me.sendMsgDisabled = false; window.clearInterval(interval); //停止 } }, 1000); } } })
方案二:一個元素,改變文字
HTML:
<button type="button" @click='delusercache()' :disabled="sendMsgDisabled" v-text="btnText"></button> //倒計時按鈕禁用:disabled="sendMsgDisabled
JS:
var vm = new Vue({ el: '#example', data() { return { time: 60, // 發(fā)送驗(yàn)證碼倒計時 sendMsgDisabled: false //按鈕可用 } }, methods: { time(){ this.sendMsgDisabled= true; //按鈕不可用 let interval = window.setInterval(()=> { this.btnText = this.time + 's重新發(fā)送' if ((this.time--) <= 0) { this.time = 120; this.btnText ='發(fā)送驗(yàn)證碼' this.sendMsgDisabled= false; //按鈕可用 window.clearInterval(interval); } }, 1000); } })
React
引用塊內(nèi)容
time = () => { this.setState({ times: this.state.times-1, btnText: '' + this.state.times + 's重新發(fā)送)', // discodeBtn: false, clearInterval:true }) var siv = setInterval(() => { this.setState({ times: this.state.times-1, btnText: '' + this.state.times + 's重新發(fā)送)', // discodeBtn: false, clearInterval:true }, () => { if (this.state.times === 0) { clearInterval(siv); this.setState({ times: 60, btnText: '發(fā)送驗(yàn)證碼', // discodeBtn: true, clearInterval:false, phone:false, isDisabled:false }) } }); }, 1000); }; <button className={(this.state.clearInterval ? 'send-btn-disabled-m' : 'send-btn-default')} disabled={this.state.isDisabled} onClick={this.getPhone} > {this.state.btnText} </button>
以上是“vue、react如何實(shí)現(xiàn)倒計時效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。