SpeechRecognition: Sự kiện error
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Sự kiện error của đối tượng SpeechRecognition trong Web Speech API được kích hoạt khi xảy ra lỗi nhận dạng giọng nói.
Cú pháp
Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc thiết lập một thuộc tính trình xử lý sự kiện.
js
addEventListener("error", (event) => { })
onerror = (event) => { }
Loại sự kiện
Một SpeechRecognitionErrorEvent. Kế thừa từ Event.
Thuộc tính sự kiện
Ngoài các thuộc tính được liệt kê bên dưới, các thuộc tính từ giao diện cha, Event, cũng khả dụng.
SpeechRecognitionErrorEvent.errorRead only-
Trả về loại lỗi được ném ra.
SpeechRecognitionErrorEvent.messageRead only-
Trả về một thông báo mô tả chi tiết hơn về lỗi.
Ví dụ
Bạn có thể sử dụng sự kiện error trong phương thức addEventListener:
js
const recognition = new (SpeechRecognition || webkitSpeechRecognition)();
recognition.addEventListener("error", (event) => {
console.error(`Speech recognition error detected: ${event.error}`);
});
Hoặc sử dụng thuộc tính trình xử lý sự kiện onerror:
js
recognition.onerror = (event) => {
console.error(`Speech recognition error detected: ${event.error}`);
};
Đặc tả kỹ thuật
| Specification |
|---|
| Web Speech API> # eventdef-speechrecognition-error> |
| Web Speech API> # dom-speechrecognition-onerror> |