vue如何設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件

這篇文章將為大家詳細(xì)講解有關(guān)vue如何設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司是專業(yè)的海林網(wǎng)站建設(shè)公司,海林接單;提供成都做網(wǎng)站、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行海林網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

簡(jiǎn)介:

倒計(jì)時(shí)秒殺組件在電商網(wǎng)站中層出不窮  不過思路萬變不離其蹤,我自己根據(jù)其他資料設(shè)計(jì)了一個(gè)vue版的

核心思路:

  1. 1、時(shí)間不能是本地客戶端的時(shí)間  必須是服務(wù)器的時(shí)間這里用一個(gè)settimeout代替 以為時(shí)間必須統(tǒng)一 

  2. 2、開始時(shí)間,結(jié)束時(shí)間通過父組件傳入,當(dāng)服務(wù)器時(shí)間在這個(gè)開始時(shí)間和結(jié)束時(shí)間的范圍內(nèi)  參加活動(dòng)按鈕可以點(diǎn)擊,并且參加過活動(dòng)以后不能再參加,

  3. 3、在組件創(chuàng)建的時(shí)候 同步得到現(xiàn)在時(shí)間服務(wù)時(shí)間差,并且在這里邊設(shè)置定時(shí)器,每秒都做判斷看秒殺是否開始和結(jié)束,

  4. 4、在更新時(shí)間的函數(shù)中是否開始和結(jié)束,

  5. 5、在computed鉤子中監(jiān)聽disable 確定按鈕是否可點(diǎn)擊

  6. 6、參加過活動(dòng)在updated中停止定時(shí)器的計(jì)時(shí),頁面銷毀的時(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)擊,
        // 初始化通過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="已參加過活動(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);//開始時(shí)間和服務(wù)器時(shí)間之差
        const diffEnd=this.endTime.diff(now);//結(jié)束時(shí)間和服務(wù)器時(shí)間之差
        if(diffStart<0){
          this.start=true;
          this.tip="秒殺已開始";
          this.btnText="參加"
        }else{
          this.tip=`距離秒殺開始還剩${Math.ceil(diffStart/1000)}秒`;
          this.btnText="活動(dòng)未開始";
        }
        if(diffEnd<=0){
          this.end=true;
          if( !this.btnText==="已參加過活動(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>
    <h2 >設(shè)計(jì)一個(gè)秒殺倒計(jì)時(shí)的組件</h2>
    <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>

關(guān)于“vue如何設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)站題目:vue如何設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件
本文鏈接:http://muchs.cn/article48/gdidhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)用戶體驗(yàn)、全網(wǎng)營銷推廣、動(dòng)態(tài)網(wǎng)站云服務(wù)器、App開發(fā)

廣告

聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作