-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add expires in count down for promptpay QR
- Loading branch information
Showing
2 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(function () { | ||
let countDownInterval; | ||
|
||
function calculateCountdown() { | ||
const currentDateTime = new Date(); | ||
const expiresAtDateTime = new Date(omise.qr_expires_at); | ||
const difference = expiresAtDateTime - currentDateTime; | ||
const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | ||
const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)); | ||
const seconds = Math.floor((difference % (1000 * 60)) / 1000); | ||
return { hours, minutes, seconds }; | ||
} | ||
|
||
function padZero(num) { | ||
return num.toString().padStart(2, '0'); | ||
} | ||
|
||
function updateCountdown(fromInterval = true) { | ||
const countdownDisplay = document.getElementById('countdown'); | ||
if(!countdownDisplay) { | ||
return; | ||
} | ||
|
||
const { hours, minutes, seconds } = calculateCountdown(); | ||
|
||
if (hours + minutes + seconds < 0) { | ||
// To prevent infinite loading, we need to reload and clear interval | ||
// only when it is from setInterval function. | ||
if (fromInterval) { | ||
clearInterval(countDownInterval) | ||
window.location.reload() | ||
} | ||
return; | ||
} | ||
|
||
countdownDisplay.innerHTML = `${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`; | ||
if (!countDownInterval) { | ||
countDownInterval = setInterval(updateCountdown, 1000); | ||
} | ||
} | ||
|
||
updateCountdown(false) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters