静态方法 Atomics.wait() 确保了一个在 Int32Array 数组中给定位置的值没有发生变化、仍然是给定的值时进程将会睡眠,直到被唤醒或超时。该方法返回一个字符串,值为"ok", "not-equal", 或 "timed-out" 之一。
注意: 这项操作仅允许同一个共享内存的 Int32Array 配合使用并且无法运行在主线程中。
语法
Atomics.wait(typedArray, index, value[, timeout])
参数
-
typedArray -
一个共享内存的
Int32Array数组。 -
index -
给定需要检测的
typedArray数组的位置索引。 -
value - 给定需要检测的位置索引的预期值。
-
timeout可选 -
超时前等待的毫秒数。
Infinity, 如未提供该参数,将为无穷大。
返回值
一个 String 字符串,值为 "ok", "not-equal", 或 "timed-out" 三种之一。
异常
- 如果参数
typedArray不是一个共享内存的TypeError。 - 如果参数
index超出了参数typedArray的边界,将会抛出一个RangeError。
示例
创建一个共享内存的 Int32Array :
var sab = new SharedArrayBuffer(1024); var int32 = new Int32Array(sab);
检测给定的数组索引0的值,如果它如预期一般的等于我们给定的值0,则这个读取线程将会睡眠等待。一旦当有一个写入线程在这个位置存储了一个新值,它将会收到写入线程的通知并且返回新值 (123) :
Atomics.wait(int32, 0, 0); console.log(int32[0]); // 123
一旦某个写入线程存储了一个新值到int32 的索引0位置,则通知给该等待线程:
console.log(int32[0]); // 0; Atomics.store(int32, 0, 123); Atomics.notify(int32, 0, 1);
规范
| 规范 | 状态 | 备注 |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) Atomics.wait |
Draft | Initial definition in ES2017. |
浏览器支持
本页的支持列表生成自结构化的数据。如果你希望为这个数据做出贡献, 请检出
https://github.com/mdn/browser-compat-data 并发送一个拉取请求给我们。
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
wait |
Chrome Full support 68
|
Edge No support 16 — 17
|
Firefox Full support 57
|
IE No support No | Opera No support No | Safari No support 10.1 — ? | WebView Android No support 60 — 63
|
Chrome Android No support 60 — 63
|
Firefox Android Full support 57
|
Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No
|
nodejs Full support 8.10.0 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.
- Uses a non-standard name.
- Uses a non-standard name.