<div class="verification-code-wrapper">
<input type="text" placeholder="六位數(shù)字驗(yàn)證碼" ref="phone_login_verification_code">
<a class="get-code"
@click="get_verification_code(phone_login, type=3)">獲取驗(yàn)證碼</a>
<div class="count-down" v-show="does_count_down_show">
{{time_60}}{{count_down}}秒后重新獲取
</div>
</div>
computed: {
count_down: function () {
// 查一下cookie中 驗(yàn)證碼字段 是不是存在
let cookie_arr = document.cookie.split(';');
let expires;
for (let i = 0; i < cookie_arr.length; i++) {
let cookieArrTrim = cookie_arr[i].trim();
if (cookieArrTrim.indexOf('count_down') === 0) {
expires = cookieArrTrim.substring('count_down'.length + 1, cookieArrTrim.length);
}
}
if (!expires) {
return null;
}
window.setInterval(() => {
this.time_60 -= 1;
}, 1000);
},
does_count_down_show: function () {
let cookie_arr = document.cookie.split(';');
let expires;
for (let i = 0; i < cookie_arr.length; i++) {
let cookieArrTrim = cookie_arr[i].trim();
if (cookieArrTrim.indexOf('count_down') === 0) {
expires = expires = cookieArrTrim.substring('count_down'.length + 1, cookieArrTrim.length);
}
}
if (this.time_60 > 0 && expires) {
return true;
}
else {
return false;
}
}
},
點(diǎn)擊事件中,從后臺(tái)取回驗(yàn)證碼后,會(huì)以當(dāng)前時(shí)間+60s當(dāng)做一個(gè)cookie存儲(chǔ)。
現(xiàn)在的問(wèn)題是:一般的驗(yàn)證碼重新發(fā)送倒計(jì)時(shí)是點(diǎn)擊了【發(fā)送】后立刻變換文字,開(kāi)始倒計(jì)時(shí)。
我這個(gè)點(diǎn)擊了發(fā)送后沒(méi)有變化,必須重新刷新一次才會(huì)開(kāi)始倒計(jì)時(shí)。
請(qǐng)問(wèn)應(yīng)該怎么改才能立刻開(kāi)始渲染?
也許需要在點(diǎn)擊事件中寫一個(gè)flag,綁定到發(fā)送按鈕,watch一下?
代碼有重復(fù)的,因?yàn)樵臼且粋€(gè)方法,我分拆了一下實(shí)驗(yàn),見(jiàn)諒。
I have already solved this problem, there are too many conditions which unnecessary,I make it brief.
computed方法會(huì)在頁(yè)面初始化的時(shí)候執(zhí)行一次,第一次應(yīng)該是被return掉才執(zhí)行setinterval,然后是函數(shù)內(nèi)部中使用的data中的屬性發(fā)生變化的時(shí)候才會(huì)執(zhí)行,你點(diǎn)擊的事件并沒(méi)有改變time_60 這個(gè)值,所以count_down并沒(méi)有執(zhí)行,刷新后應(yīng)為cook中有值,沒(méi)有被return 才會(huì)運(yùn)行setinterval;1樓說(shuō)的對(duì),這樣的邏輯不適合寫在計(jì)算屬性中,應(yīng)該定義在methods中,在掉接口的回調(diào)中運(yùn)行比較合適。
還有個(gè)問(wèn)題count_down中沒(méi)有return 任何有效值,看你的邏輯,應(yīng)該只會(huì)展示undefined和null.
<div class="count-down" v-if="countDownShow">{{time}}秒后重新獲取</div>
data() {
return{
countDownShow: false,
time:60
}
},
methods:{
countDown: function () {
// 查一下cookie中 驗(yàn)證碼字段 是不是存在
let cookie_arr = document.cookie.split(';');
let expires;
for (let i = 0; i < cookie_arr.length; i++) {
let cookieArrTrim = cookie_arr[i].trim();
if (cookieArrTrim.indexOf('count_down') === 0) {
expires = cookieArrTrim.substring('count_down'.length + 1, cookieArrTrim.length);
}
}
if (!expires && this.time > 0) {
this.countDownShow = true
}
let timeInter;
timeInter = window.setInterval(() => {
if(this.time === 0){
clearInteral(timeInter)
}else{
this.time_60 -= 1;
}
}, 1000);
},
}
獲取完驗(yàn)證碼后執(zhí)行 this.countDown()方法即可。
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。