diff options
author | missionfloyd <missionfloyd@users.noreply.github.com> | 2023-05-07 04:00:13 +0000 |
---|---|---|
committer | missionfloyd <missionfloyd@users.noreply.github.com> | 2023-05-07 04:00:13 +0000 |
commit | cca5782d183c389e46a822a6e0abc4f8fd2d8df8 (patch) | |
tree | 4265b1022a9b3c46ad9f2133cb7a45185fe7db73 | |
parent | 5cbc1c5d438e9ca7384a26eab28a09f44c5162f2 (diff) | |
download | stable-diffusion-webui-gfx803-cca5782d183c389e46a822a6e0abc4f8fd2d8df8.tar.gz stable-diffusion-webui-gfx803-cca5782d183c389e46a822a6e0abc4f8fd2d8df8.tar.bz2 stable-diffusion-webui-gfx803-cca5782d183c389e46a822a6e0abc4f8fd2d8df8.zip |
Improve joypad performance
-rw-r--r-- | javascript/imageviewerGamepad.js | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/javascript/imageviewerGamepad.js b/javascript/imageviewerGamepad.js index db932bca..f25f0857 100644 --- a/javascript/imageviewerGamepad.js +++ b/javascript/imageviewerGamepad.js @@ -1,15 +1,27 @@ -const delay = 250//ms +const delay = 350//ms +let isWaiting = false; window.addEventListener('gamepadconnected', (e) => { - setInterval(() => { - if (!opts.js_modal_lightbox_gamepad) return; + setInterval(async () => { + if (!opts.js_modal_lightbox_gamepad || isWaiting) return; const gamepad = navigator.getGamepads()[0]; const xValue = gamepad.axes[0]; - if (xValue < -0.3) { + if (xValue <= -0.3) { modalPrevImage(e); - } else if (xValue > 0.3) { + isWaiting = true; + } else if (xValue >= 0.3) { modalNextImage(e); + isWaiting = true; } - }, delay); + if (isWaiting) { + await sleepUntil(() => { + const xValue = navigator.getGamepads()[0].axes[0] + if (xValue < 0.3 && xValue > -0.3) { + return true; + } + }, delay); + isWaiting = false; + } + }, 10); }); /* @@ -31,3 +43,15 @@ window.addEventListener('wheel', (e) => { isScrolling = false; }, 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); + }); +} |