js原生方法实现语音播放【vue项目中】

可循环播放三次

<button @click="play">语音播放</button>

data() {
    return {
        count:0;
    }
}

cyclePlay(){
       var time = new Date();
      console.log('time:',time)
      const asd = '请001号前来办理业务'
      const msg = new SpeechSynthesisUtterance()
      msg.text = asd
      let that = this
      msg.onend = function(){
        that.count+=1
        // alert('播放结束')
        setTimeout(function(){
      that.play()

        },1000)
      }
      window.speechSynthesis.speak(msg)
    },
    play(){
      console.log('this.count',this.count)
      if(this.count < 3){
        this.cyclePlay()
      }else{
        this.count = 0
        console.log('播放完毕')
      }
      // setInterval(this.cyclePlay(),1000)
    },    

 

posted @ 2023-03-15 09:47  小那  阅读(432)  评论(0编辑  收藏  举报