Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Wait until html text is replaced and loaded

I have a website that has a translate button. When the user clicks it, I use document.getElementsByClassName.innerHTML to change the language of all the html text.

However, I want my loading animation to show after the user clicks translate, until all the text has been replaced, and the language-specific font has loaded in as well.

How could I wait until this happens before turning off my loading animation visibility? As far as I understand, the DOM has already loaded when the user clicks translate.

Edit: I should've added some code initially. Here is a small sample of what I'm doing:

index.html

<div>
    <h1 class="1.1.1">My website</h1>
    <h1 class="1.1.2">Text sample 1</h1>
</div>

translate.js

function setEnglish() {
    document.getElementsByClassName("1.1.1")[0].innerHTML = "My website";
    document.getElementsByClassName("1.1.2")[0].innerHTML = "Text sample 1";
}
function setFrench() {
    document.getElementsByClassName("1.1.1")[0].innerHTML = "Mon site-web";
    document.getElementsByClassName("1.1.2")[0].innerHTML = "Exemple de texte 1";
}

There is a button in index.html that when pressed, calls a language toggle function (I'm only switching between two languages). I use simple logic to switch between the languages in translate.js.

So unfortunately, I have to repeat the English (main language) translation in the translate.js even though it exists in the html, because I need to toggle back to English from French.

I also have a font for the English translation and a font for the French translation that is changed in translate.js:

if language is English:

var body = document.getElementsByTagName('body')[0];
body.style.fontFamily = "font1";

if language is French:

var body = document.getElementsByTagName('body')[0];
body.style.fontFamily = "font2";

The problem is that when the user clicks the button, the text loads, but the font change is delayed, so the user sees the font change.

Currently, I have a loading animation that triggers every time the user clicks the translate button, but its set to last for a hard delay of 500ms (regardless of how quickly the font loads). After the time is up, the loading gif is hidden.

I want to delay the hiding of this loading animation until the font and text are done switching from the previous language, instead of having a constant delay. Sometimes, the loading animation expires, then the font changes because it hasn't loaded fast enough. Hope this clears things up.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!