ES6/TS掃描器-定時(shí)器工具類 可暫停,列表循環(huán)

最近做了一個(gè)中央空調(diào)的demo,用了很多的定時(shí)器,然后我用數(shù)據(jù)驅(qū)動(dòng)的思想給予了定時(shí)器等同于暫停/繼續(xù)列表循環(huán)的功能。為了便于理解,以及之后的復(fù)用,我封裝了一個(gè)工具類。
同樣,雖然用了TS但改成純ES6也很好改的。

export class interval {
  suspend: boolean // 暫停指示器
  pauseDo: Function // 每次暫停時(shí)會(huì)執(zhí)行一次的方法
  goonDo: Function // 每次繼續(xù)時(shí)會(huì)執(zhí)行一次的方法
  constructor() {
    this.suspend = false
    this.pauseDo = null
    this.goonDo = null
  }
  static isExist(data): boolean {
    return data !== null || typeof(data) !== 'undefined'
  }
  // 初始化,設(shè)置定時(shí)器
  init(method,cycle){
    let self = this
    setInterval(function () {
      if(self.suspend){
        return
      }
      method()
    },cycle)
  }
  // 定時(shí)器暫停,可傳入?yún)?shù)作為pauseDo的參數(shù)
  pause(...params: any) {
    this.suspend = true // 暫停
    if(this.pauseDo != null) {
      this.pauseDo(...params)
    }
  }
  // 定時(shí)器暫停,可傳入?yún)?shù)作為goonDo的參數(shù)
  goon(...params: any) {
    this.suspend = false // 繼續(xù)
    if(this.goonDo != null) {
      this.goonDo(...params)
    }
  }
}

列表循環(huán)定時(shí)器子類:

export class listInterval extends interval{
    count: number  // 時(shí)間輔助計(jì)數(shù)器
    constructor() {
      super()
      this.count = 0
    }
    // 列表內(nèi)元素依次相加,返回和
    listAdd(list) {
      let result = 0
      for(let item of list){
        result += item
      }
      return result
    }
    // 列表內(nèi)元素依次相加,加到index位置為止,返回和
    addToIndex(list,index){
      let result = 0
      if (index == list.length-1){
        return result  // 余數(shù)為0的情況
      }
      for(let index1 in list){
        result += list[index1]
        if (index1 === index){
          return result
        }
      }
    }
    // 下面是數(shù)學(xué)問題,重在理解,不贅述了
    init(methodList,cycleList){
      let self = this
      for(let index in cycleList){
        cycleList[index] = cycleList[index]/500
      }
      setInterval(function () {
        if(self.suspend){
          return
        }
        self.count++
        let bigCycle = self.listAdd(cycleList)
        for(let index in cycleList){
          if(self.count%bigCycle === self.addToIndex(cycleList,index)){
            methodList[index]()
          }
        }
      },500)  // 默認(rèn)0.5秒掃描一次暫停指示器,掃描間隔越小越精確
    }
}

使用例:每秒輸出一個(gè)數(shù)字二,每輸出3次后,暫停3秒再繼續(xù),

    let pauseWord = '暫停了'
    let goonWord = '繼續(xù)了'
    const testTimer = new interval()
    testTimer.init(function () {
      console.log(2)
    },1000)
    testTimer.pauseDo = function(a){
      console.log(a)
    }
    testTimer.goonDo = function(a){
      console.log(a)
    }

    const testListTimer = new listInterval()
    let methodList = []
    methodList.push(function () {
      testTimer.pause(pauseWord)
    })
    methodList.push(function () {
        testTimer.goon(goonWord)
    })
    testListTimer.init(methodList,[3000,3000])
  }

有疑問可在下方留言、或在gitee上提交Issuse,
前往gitee了解更多

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容