在我看来,你只想在加载数据后删除预加载程序。
jQuery的
.load()
函数支持在请求完成后执行的回调函数
jQuery(function($) {
$("#balance").load("stripebalance.php", function() {
$("#preloader").fadeOut();
});
});
在这个过程中不需要任何间隔或超时。
此外,现在快2023年了,你当然不需要jQuery
const getStripeBalance = async () => {
const res = await fetch("stripebalance.php");
if (!res.ok) {
throw new Error(`Fetch error: ${res.status} - ${res.statusText}`);
}
return res.text();
}
const stripeBalanceHtmlPromise = getStripeBalance();
document.addEventListener("DOMContentLoaded", async () => {
const balance = document.getElementById("balance");
const loader = document.getElementById("preloader");
const stripeBalanceHtml = await stripeBalanceHtmlPromise;
balance.innerHTML = stripeBalanceHtml;
loader.addEventListener("animationend", () => {
loader.remove();
});
loader.classList.add("fadeOut");
});
.fadeOut {
animation: fadeOut 0.5s linear forwards;
}
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}