Atomics notify()
notify 方法通知等待的代理唤醒。 notify 方法只能与使用 SharedArrayBuffer 创建的 Int32Array 一起使用。如果使用非共享 ArrayBuffer 对象,则返回 0。
语法
Atomics.notify(typedArray, index, count)
参数
typedArray 是一个共享的 Int32Array。
index 是 typedarray 中要唤醒的位置。
count 是要通知的休眠代理的数量。
返回
返回被唤醒的代理数量。
例外
TypeError 如果传递的数组不是整数类型数组。
RangeError 如果传递的索引在类型化数组中超出范围。
示例
以下是实现 JavaScript Atomics 的代码-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Atomics Example</title>
<style>
.result {
font-size: 20px;
border: 1px solid black;
}
</style>
</head>
<body onLoad="operate();">
<h1>JavaScript Atomics Properties</h1>
<div class="result"></div>
<p>Atomics.store(arr, 0, 5)</p>
<p>Atomics.notify(arr, 0, 1)</p>
<script>
function operate(){
let container = document.querySelector(".result");
// create a SharedArrayBuffer
var buffer = new SharedArrayBuffer(16);
var arr = new Int32Array(buffer);
// Initialise element at zeroth position of array with 6
arr[0] = 6;
container.innerHTML = Atomics.store(arr, 0, 5) + '<br>' + Atomics.notify(arr, 0, 1);
}
</script>
</body>
</html>