Improve joypad performance

This commit is contained in:
missionfloyd 2023-05-06 22:00:13 -06:00
parent 5cbc1c5d43
commit cca5782d18

View File

@ -1,15 +1,27 @@
const delay = 250//ms const delay = 350//ms
let isWaiting = false;
window.addEventListener('gamepadconnected', (e) => { window.addEventListener('gamepadconnected', (e) => {
setInterval(() => { setInterval(async () => {
if (!opts.js_modal_lightbox_gamepad) return; if (!opts.js_modal_lightbox_gamepad || isWaiting) return;
const gamepad = navigator.getGamepads()[0]; const gamepad = navigator.getGamepads()[0];
const xValue = gamepad.axes[0]; const xValue = gamepad.axes[0];
if (xValue < -0.3) { if (xValue <= -0.3) {
modalPrevImage(e); modalPrevImage(e);
} else if (xValue > 0.3) { isWaiting = true;
} else if (xValue >= 0.3) {
modalNextImage(e); modalNextImage(e);
isWaiting = true;
}
if (isWaiting) {
await sleepUntil(() => {
const xValue = navigator.getGamepads()[0].axes[0]
if (xValue < 0.3 && xValue > -0.3) {
return true;
} }
}, delay); }, delay);
isWaiting = false;
}
}, 10);
}); });
/* /*
@ -31,3 +43,15 @@ window.addEventListener('wheel', (e) => {
isScrolling = false; isScrolling = false;
}, delay); }, delay);
}); });
function sleepUntil(f, timeout) {
return new Promise((resolve) => {
const timeStart = new Date();
const wait = setInterval(function() {
if (f() || new Date() - timeStart > timeout) {
clearInterval(wait);
resolve();
}
}, 20);
});
}