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

254
Views
How to force restart a javascript function

Hello i've script which changes me text colors on interval, there is:

  <script>
  const txts=document.querySelector(".animate-text").children,
        txtsLen=txts.length;
    let index=0;
   const textInTimer=3500,
         textOutTimer=3300;

  function animateText() {
     for(let i=0; i<txtsLen; i++){
       txts[i].classList.remove("text-in","text-out");
     }
     txts[index].classList.add("text-in");

     setTimeout(function(){

     },textOutTimer)

     setTimeout(function(){


     },textInTimer);
  }

  window.onload=animateText;

  </script>

  <script>
  var i = 0;
  function changeClass(){
    setTimeout(function() {

      txts[index].classList.add("text-out");
      if(index == txtsLen-1){
          index=0;
        }
       else{
           index++;
         }
        animateText();

      $("#animate-button").removeClass("anim" + i)
          i = (i==4)?1:i+1;
      $("#animate-button").addClass("anim" + (i));
    }, 300)


  }
  setInterval(changeClass, 3500);

  windows.onload=changeClass;

  </script>

I need to know how can i force to start over this script after switch back to my page.

So next script for restart looks:

  <script>
document.addEventListener("visibilitychange", function() {
   if (document.hidden){
       console.log("Browser tab is hidden")
   } else {
       console.log("Browser tab is visible")
       //Resetowanie slidera
       jQuery('#carouselExampleControls').carousel(0);
       console.log('Slider został przywrócony do 1 slajdu');
       // start over changeClass function here

       console.log('Funkcja changeClass zaczęła działać od początku');
   }
});
</script>

I need to start over changeClass() function and put it below the comment in last script

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!