承诺在setTimeOut

时间:2019-06-03 06:16:53

标签: javascript angular promise

我使用的是cordova的语音识别插件,其中在用户单击语音按钮startRecognition之后单击会被触发,但是如果在10000毫秒后如果用户不讲话,则应该触发stopListening()函数,然后继续除非用户说退出,否则将连续播放。

我正在使用包装在一个Promise中的setTimeout,当该Promise被解决时,我正在解决Promise,这将触发cordova插件进行语音识别

除非用户退出,否则此代码将连续运行语音识别。

  listenMsgAndReplyRepeat() {
    const self = this;
    this.listenMsg().then(res => {
      alert('got result ' + res);
      if (res === 'Exit') {
        self.exitFromApp();
      } else if (res === 'Success') {
        self.listenMsgAndReplyRepeat();
      } else if (res === 'Stopped') {
        alert('the speech recognition was stopped');
        self.botReply('Hello? Do you Want me to Exit?');
        self.listenMsgAndReplyRepeat();
      } else {
        self.botReply('Some error occured will detecting speech');
      }
    });
  }

此代码是开始语音识别的代码

listenMsg() {
    const self = this;
    // Verify if recognition is available
    return new Promise(function(resolve, reject) {
      self
        .isRecognitionAvailable()
        .then(function(available) {
          if (available) {
            return window.plugins.speechRecognition.hasPermission();
          }
        })
        .then(function(hasPermission) {
          function startRecognition() {
            self.speakPopUp = true;
            return self
              .startRecognition({
                language: 'en-US',
                showPopup: false,
                matches: 2
                // showPartial: true
              })
              .then(function(result) {
                clearTimeout(self.stopListen);
                self.speakPopUp = false;
                if (result[0] === 'exit') {
                    resolve('Exit'); //working
                }
                self.botReply(result[0]).then(res => {
                  resolve('Success'); //working
                });
              })
              .catch(function(err) {
                reject('Error');
              });
          }
          if (!hasPermission) {
            self
              .requestPermission()
              .then(function() {
                self.stopSpeech().then(res => {
                  self.speakPopUp = false;
                  resolve('Stopped');  //this resolve is not working
                });
                startRecognition();
              })
              .catch(function(err) {
                console.error('Cannot get permission', err);
                reject('Error');
              });
          } else {
            self.stopSpeech().then(res => {
              self.speakPopUp = false;
              resolve('Stopped');  //this resolve is not working
            });
            startRecognition();
          }
        })
        .catch(function(err) {
          console.error(err);
          reject('Error');
        });
    });
  }

本地函数startRecognition中的resolve起作用,但if else条件中的resolve不能起作用

这是stopRecognition代码

  stopSpeech() {
    const self = this;
    return new Promise(function(resolve, reject) {
      self.stopListen = setTimeout(() => {
        self.stopListening().then(res => {
          clearTimeout(self.stopListen);
          resolve('stopped');
        });
      }, 10000);
    });
  }

setTimeOut内部的解析正在起作用。

我将setTimeOut分配给变量,因为如果用户讲话,我必须清除它,以免触发stopListening

1 个答案:

答案 0 :(得分:0)

我能够使用Promise.all()解决此问题。我将超时包装在一个promise中,并将其传递到promise数组中,然后我将stopListening诺言推送给了它。

const promises = [];
promises.push(self.delay(10000));
promises.push(self.stopListening());
Promise.all(promises).then((res) => {
   self.speakPopUp = false;
   resolve('Stopped');
  });
  delay(t) {
    return new Promise(function(resolve) {
        setTimeout(resolve, t);
    });