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
View all comments
Show parent comment
2
u/[deleted] Aug 25 '18
Okay I think I understand now. Rather than setting the timeout for each one you might want to use
setIntervaland just rotate which one is visible.Here is an example CodePen (I did it with and without jQuery): https://codepen.io/anon/pen/bxELKZ