1 回答

TA贡献1797条经验 获得超6个赞
我找到了解决方案,我需要一些东西来检测并确认wasm已加载并准备好进行处理,这与 JS 中用于检查文档是否准备就绪的方法相同:
if (document.readyState === 'complete') {
// The page is fully loaded
}
// or
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
// document ready
}
};
因此,作为wasm我的代码中的启动函数,我async在 JS 中使用了以下内容:
<!DOCTYPE html>
<html>
<head>
<title>WASM</title>
<!-- WASM -->
<script src="http://localhost:8080/www/wasm_exec.js"></script>
<script src="http://localhost:8080/www/loadWasm.js"></script>
</head>
<body>
</body>
<script>
(async () => {
try {
await init();
alert("Wasm had been loaded")
console.log(multiply(5, 3));
} catch (e) {
console.log(e);
}
})();
/***** OR ****/
(async () => {
await init();
alert("Wasm had been loaded")
console.log(multiply(5, 3));
})().catch(e => {
console.log(e);
});
/*************/
</script>
</html>
这帮助我确定文档已准备好处理并调用 wasm 函数。
wasm加载函数简单地变成了:
async function init(){
const go = new Go();
const result = await WebAssembly.instantiateStreaming(
fetch("http://localhost:8080/www/main.wasm"),
go.importObject
);
go.run(result.instance);
}
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报