Angular语音识别功能被多次调用。
创始人
2024-10-30 19:00:35
0

可以通过使用rxjs库中的debounceTime操作符来解决这个问题。该操作符将一个可观察序列的发射值推迟一段时间,只发出最后一个值。

例子:

import { Component, OnInit, OnDestroy } from '@angular/core'; import { Observable, Subscription } from 'rxjs/Rx';

@Component({ selector: 'app-my-component', templateUrl: 'my.component.html' }) export class MyComponent implements OnInit, OnDestroy { speechSubscription: Subscription;

ngOnInit() { // 创建可观察序列 const speechObservable = Observable.fromEvent(document, 'speechresult') .map((event: any) => { return event.results[0][0].transcript.trim(); });

// debounceTime操作符延迟时间设置为500毫秒
this.speechSubscription = speechObservable.debounceTime(500)
  .subscribe((text: string) => {
    console.log('识别的文本:', text);
    // 处理识别的文本
  });

}

ngOnDestroy() { this.speechSubscription.unsubscribe(); } }

相关内容

热门资讯

曝光透视!哈糖大菠萝有挂吗!真... 曝光透视!哈糖大菠萝有挂吗!真是有辅助方法(发现有挂)-哔哩哔哩1、在哈糖大菠萝有挂吗插件功能辅助器...
必备透视!菠萝德普辅助器免费版... 必备透视!菠萝德普辅助器免费版在哪里!一贯存在有辅助插件(有挂功能)-哔哩哔哩该软件可以轻松地帮助玩...
解迷透视!sohoopoker... 解迷透视!sohoopoker辅助!本来真的是有辅助软件(讲解有挂)-哔哩哔哩一、sohoopoke...
分享透视!xpoker怎么作必... 分享透视!xpoker怎么作必弊!原来一直总是有辅助脚本(真的有挂)-哔哩哔哩1、进入游戏-大厅左侧...
解密透视!pokermaste... 解密透视!pokermaster修改器!其实真的有辅助技巧(证实有挂)-哔哩哔哩1、下载好poker...
了解透视!拱趴大菠萝万能挂图解... 了解透视!拱趴大菠萝万能挂图解!一贯真的是有辅助神器(发现有挂)-哔哩哔哩1、下载好拱趴大菠萝万能挂...
科普透视!约局吧开挂!果然有辅... 您好,约局吧开挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这...
揭幕透视!pokermaste... 揭幕透视!pokermaster脚本!竟然是有辅助app(有挂方略)-哔哩哔哩1、pokermast...
关于透视!newpoker怎么... 关于透视!newpoker怎么安装脚本!总是一直都是有辅助插件(有挂实锤)-哔哩哔哩1、超多福利:超...
详细透视!约局吧app有挂吗!... 详细透视!约局吧app有挂吗!真是是真的有辅助神器(有挂规律)-哔哩哔哩1、任何约局吧app有挂吗透...