diff options
| author | かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> | 2024-10-23 14:23:29 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-23 14:23:29 +0900 |
| commit | 15ae1605ec199792cd651073b1c6de480a7eeabe (patch) | |
| tree | 5a9105ec961223e1bf43165e276f2217f5873dde /packages/frontend/src/boot | |
| parent | Merge commit from fork (diff) | |
| download | sharkey-15ae1605ec199792cd651073b1c6de480a7eeabe.tar.gz sharkey-15ae1605ec199792cd651073b1c6de480a7eeabe.tar.bz2 sharkey-15ae1605ec199792cd651073b1c6de480a7eeabe.zip | |
enhance(frontend): 「単なるラッキー」の調整 (#14807)
* enhance(frontend): 「単なるラッキー」の調整
* refactor
* comment
* Update Changelog
---------
Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
Diffstat (limited to 'packages/frontend/src/boot')
| -rw-r--r-- | packages/frontend/src/boot/main-boot.ts | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/packages/frontend/src/boot/main-boot.ts b/packages/frontend/src/boot/main-boot.ts index 2392381b64..2bf9029479 100644 --- a/packages/frontend/src/boot/main-boot.ts +++ b/packages/frontend/src/boot/main-boot.ts @@ -231,11 +231,41 @@ export async function mainBoot() { } if (!claimedAchievements.includes('justPlainLucky')) { - window.setInterval(() => { + let justPlainLuckyTimer: number | null = null; + let lastVisibilityChangedAt = Date.now(); + + function claimPlainLucky() { + if (document.visibilityState !== 'visible') { + if (justPlainLuckyTimer != null) window.clearTimeout(justPlainLuckyTimer); + return; + } + if (Math.floor(Math.random() * 20000) === 0) { claimAchievement('justPlainLucky'); + } else { + justPlainLuckyTimer = window.setTimeout(claimPlainLucky, 1000 * 10); } - }, 1000 * 10); + } + + window.addEventListener('visibilitychange', () => { + const now = Date.now(); + + if (document.visibilityState === 'visible') { + // タブを高速で切り替えたら取得処理が何度も走るのを防ぐ + if ((now - lastVisibilityChangedAt) < 1000 * 10) { + justPlainLuckyTimer = window.setTimeout(claimPlainLucky, 1000 * 10); + } else { + claimPlainLucky(); + } + } else if (justPlainLuckyTimer != null) { + window.clearTimeout(justPlainLuckyTimer); + justPlainLuckyTimer = null; + } + + lastVisibilityChangedAt = now; + }, { passive: true }); + + claimPlainLucky(); } if (!claimedAchievements.includes('client30min')) { |