vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件詳解
簡(jiǎn)介:
倒計(jì)時(shí)秒殺組件在電商網(wǎng)站中層出不窮 不過(guò)思路萬(wàn)變不離其蹤,我自己根據(jù)其他資料設(shè)計(jì)了一個(gè)vue版的
核心思路:
- 1、時(shí)間不能是本地客戶端的時(shí)間 必須是服務(wù)器的時(shí)間這里用一個(gè)settimeout代替 以為時(shí)間必須統(tǒng)一
- 2、開(kāi)始時(shí)間,結(jié)束時(shí)間通過(guò)父組件傳入,當(dāng)服務(wù)器時(shí)間在這個(gè)開(kāi)始時(shí)間和結(jié)束時(shí)間的范圍內(nèi) 參加活動(dòng)按鈕可以點(diǎn)擊,并且參加過(guò)活動(dòng)以后不能再參加,
- 3、在組件創(chuàng)建的時(shí)候 同步得到現(xiàn)在時(shí)間服務(wù)時(shí)間差,并且在這里邊設(shè)置定時(shí)器,每秒都做判斷看秒殺是否開(kāi)始和結(jié)束,
- 4、在更新時(shí)間的函數(shù)中是否開(kāi)始和結(jié)束,
- 5、在computed鉤子中監(jiān)聽(tīng)disable 確定按鈕是否可點(diǎn)擊
- 6、參加過(guò)活動(dòng)在updated中停止定時(shí)器的計(jì)時(shí),頁(yè)面銷毀的時(shí)候也停止計(jì)時(shí)
下邊是代碼
子組件
<template>
<div>
<button @click="handleClick" :disabled="disabled">
{{btnText}}
</button>
<span>{{tip}}</span>
</div>
</template>
<script>
import moment from 'moment'
export default {
name: "Spike",
props: {
startTime: {
required: true,
validator: (val) => {
return moment.isMoment(val)
}
},
endTime: {
required: true,
validator: (val) => {
return moment.isMoment(val)
}
}
},
data() {
return {
start: false,
end: false,
done: false,
tip: '',
timeGap: 0,
btnText:""
}
},
computed: {
disabled() {
//當(dāng)三個(gè)異號(hào)的時(shí)候disable返回真,不可點(diǎn)擊,
// 初始化通過(guò)this.updateState確定disable的狀態(tài)
return !(this.start && !this.end && !this.done);
}
},
async created() {
const serverTime=await this.getServerTime();
this.timeGap=Date.now()-serverTime;//當(dāng)前時(shí)間和服務(wù)器時(shí)間差
this.updateState();
this.timeInterval=setInterval(()=>{
this.updateState()
},1000)
},
updated(){
if(this.end||this.done){
clearInterval(this.timeInterval)
}
},
methods: {
handleClick() {
alert("提交成功");
this.done=true;
this.btnText="已參加過(guò)活動(dòng)"
},
getServerTime() {
//模擬服務(wù)器時(shí)間
return new Promise((resolve, reject) => {
setTimeout(() => {
//當(dāng)前時(shí)間慢10秒就是服務(wù)器時(shí)間
resolve(new Date(Date.now() -10 * 1000).getTime())//跟本地時(shí)間差
}, 0)
})
},
updateState() {
const now = moment(new Date(Date.now() - this.timeGap));//當(dāng)前服務(wù)器時(shí)間
const diffStart=this.startTime.diff(now);//開(kāi)始時(shí)間和服務(wù)器時(shí)間之差
const diffEnd=this.endTime.diff(now);//結(jié)束時(shí)間和服務(wù)器時(shí)間之差
if(diffStart<0){
this.start=true;
this.tip="秒殺已開(kāi)始";
this.btnText="參加"
}else{
this.tip=`距離秒殺開(kāi)始還剩${Math.ceil(diffStart/1000)}秒`;
this.btnText="活動(dòng)未開(kāi)始";
}
if(diffEnd<=0){
this.end=true;
if( !this.btnText==="已參加過(guò)活動(dòng)"||this.btnText==="參加"){
this.tip="秒殺已結(jié)束";
this.btnText="活動(dòng)已結(jié)束";
}
}
}
},
beforeDestroy() {
clearInterval(this.timeInterval)
}
}
</script>
<style scoped>
button[disabled]{
cursor: not-allowed;
}
</style>
父組件
<template>
<div>
<h1 style="color: red">設(shè)計(jì)一個(gè)秒殺倒計(jì)時(shí)的組件</h1>
<Spike :startTime="startTime" :endTime="endTime"></Spike>
</div>
</template>
<script>
import Spike from './Spike'
import moment from 'moment'
export default {
name: "index",
components:{
Spike
},
data(){
return{
endTime:moment(new Date(Date.now()+10*1000)),
startTime:moment(new Date(Date.now()))
}
}
}
</script>
<style scoped>
</style>
以上所述是小編給大家介紹的vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue打包c(diǎn)hunk-vendors.js文件過(guò)大導(dǎo)致頁(yè)面加載緩慢的解決
這篇文章主要介紹了vue打包c(diǎn)hunk-vendors.js文件過(guò)大導(dǎo)致頁(yè)面加載緩慢的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)菜單映射功能方法
這篇文章主要介紹了Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)菜單映射功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
在vue中使用express-mock搭建mock服務(wù)的方法
這篇文章主要介紹了在vue中使用express-mock搭建mock服務(wù)的方法,文中給大家提到了在vue-test-utils 中 mock 全局對(duì)象的相關(guān)知識(shí) ,需要的朋友可以參考下2018-11-11
vue3封裝簡(jiǎn)易的vue-echarts問(wèn)題
這篇文章主要介紹了vue3封裝簡(jiǎn)易的vue-echarts問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

