forked from rDrama/rDrama
1
0
Fork 0

delay toast if user switches browser tab

master
Aevann 2024-02-29 23:48:49 +02:00
parent 7426158eb4
commit afcdf0fe57
1 changed files with 10 additions and 1 deletions

View File

@ -11,7 +11,7 @@ function getMessageFromJsonData(success, json) {
return message;
}
function showToast(success, message) {
function actuallyShowToast(success, message) {
const oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (success ? 'error': 'success'))); // intentionally reversed here: this is the old toast
oldToast.hide();
let element = success ? "toast-post-success" : "toast-post-error";
@ -23,6 +23,15 @@ function showToast(success, message) {
bootstrap.Toast.getOrCreateInstance(document.getElementById(element)).show();
}
function showToast(success, message) {
if (document.hasFocus())
actuallyShowToast(success, message)
else
document.addEventListener('visibilitychange', () => {
actuallyShowToast(success, message)
}, {once : true})
}
function createXhrWithFormKey(url, form=new FormData(), method='POST') {
const xhr = new XMLHttpRequest();
xhr.open(method, url);