[LayaAirIDE 2.0]解决游戏最小化退到后台后浏览器会强制1秒执行一次ILaya.stage._loop()
ILaya.m_worker = new Worker("libs/sleepThread.js");
ILaya.m_worker.onmessage = function () {
if (!ILaya.stage.isVisibility) {
// console.log("_enterFrame...ILaya.stage._loop.........2222............enterTime=", ILaya.Browser.now());
ILaya.stage._loop();
ILaya.m_worker.postMessage(['wait']);
} else {
}
};
_onVisibilitychange() {
if (!ILaya.stage.isVisibility) {
// this._timeId = window.setInterval(this._enterFrame, 1000);
ILaya.m_worker.postMessage(['wait']);
}
else if (this._timeId != 0) {
// window.clearInterval(this._timeId);
// 这里不要结束worker线程,看了下workloader的worker也没有结束
// ILaya.m_worker && ILaya.m_worker.terminate(); // 结束 worker线程
}
}
sleepThread.js:
onmessage = function (ev) {
try {
// ev.data[0]
var ms = 25;
if (typeof SharedArrayBuffer !== 'undefined' && typeof Atomics !== 'undefined') {
const nil = new Int32Array(new SharedArrayBuffer(4))
// also filters out NaN, non-number types, including empty strings, but allows bigints
const valid = ms > 0 && ms < Infinity
if (valid === false) {
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
throw TypeError('sleep: ms must be a number')
}
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
}
Atomics.wait(nil, 0, 0, Number(ms))
} else {
// also filters out NaN, non-number types, including empty strings, but allows bigints
const valid = ms > 0 && ms < Infinity
if (valid === false) {
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
throw TypeError('sleep: ms must be a number')
}
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
}
const target = Date.now() + Number(ms)
while (target > Date.now()){}
}
postMessage(['ok']);
} catch (e) {
console.log("Exception in wait onmessage fuction!");
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
0 个回复