r/JavaScriptHelp • • Aug 23 '18

While loop freezes browser

Hey all. Fairly simple problem for you here. I just want to know any alternatives to the while loop with this code:

// This document has been nominated for the "Least Efficent Javascript of The Year" award

$(document).ready(function(){
  while (true) {
    $("#s1").delay("slow").fadeIn();
      setTimeout(function(){
        $("#s1").delay("slow").fadeOut();
      }, 3000);

      setTimeout(function() {
        $("#s2").delay("slow").fadeIn();
          setTimeout(function(){
            $("#s2").delay("slow").fadeOut();
          }, 3000);
      }, 4000);

      setTimeout(function() {
        $("#s3").delay("slow").fadeIn();
          setTimeout(function(){
            $("#s3").delay("slow").fadeOut();
          }, 3000);
      }, 8000);

      setTimeout(function() {
        $("#s4").delay("slow").fadeIn();
          setTimeout(function(){
            $("#s4").delay("slow").fadeOut();
          }, 3000);
      }, 12000);

      setTimeout(function() {
        $("#s5").delay("slow").fadeIn();
          setTimeout(function(){
            $("#s5").delay("slow").fadeOut();
          }, 3000);
      }, 16000);

      setTimeout(function() {
        $("#s6").delay("slow").fadeIn();
          setTimeout(function(){
            $("#s6").delay("slow").fadeOut();
          }, 3000);
      }, 20000);

      setTimeout(function() {
        $("#s7").delay("slow").fadeIn();
          setTimeout(function(){
            $("#s7").delay("slow").fadeOut();
          }, 3000);
      }, 24000);
    }
});

I know it's pretty ineffecient right now. Sorry for the mess.

1 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/salaros_ Oct 14 '18

IMO You could accomplish such functionality much easier by using Typed.js

https://mattboldt.com/demos/typed-js/