enqueue()
方法在
ReadableStreamDefaultController
interface enqueues a given chunk in the associated stream.
readableStreamDefaultController.enqueue(chunk);
The chunk to enqueue.
undefined
.
ReadableStreamDefaultController
.
In the following simple example, a custom
ReadableStream
is created using a constructor (see our
Simple random stream example
for the full code). The
start()
function generates a random string of text every second and enqueues it into the stream — see
controller.enqueue(string)
。
cancel()
function is also provided to stop the generation if
ReadableStream.cancel()
is called for any reason.
When a button is pressed, the generation is stopped, the stream is closed using
ReadableStreamDefaultController.close()
, and another function is run, which reads the data back out of the stream.
const stream = new ReadableStream({
start(controller) {
interval = setInterval(() => {
let string = randomChars();
// Add the string to the stream
controller.enqueue(string);
// show it on the screen
let listItem = document.createElement('li');
listItem.textContent = string;
list1.appendChild(listItem);
}, 1000);
button.addEventListener('click', function() {
clearInterval(interval);
fetchStream();
controller.close();
})
},
pull(controller) {
// We don't really need a pull in this example
},
cancel() {
// This is called if the reader cancels,
// so we should stop generating strings
clearInterval(interval);
}
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
流
The definition of 'enqueue()' in that specification. |
实时标准 | 初始定义。 |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
enqueue
|
Chrome ? | Edge ? |
Firefox
65
|
IE ? | Opera ? | Safari ? | WebView Android ? | Chrome Android ? |
Firefox Android
65
|
Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
完整支持
兼容性未知
实验。期望将来行为有所改变。
用户必须明确启用此特征。
ReadableStreamDefaultController