Desired output: Animate the wave forms, but use less resources.
Any bugs: There are no bugs that I know of.
I tied searching google for "use less setIntervals in my project" and found this code
function interval(func, wait, times){ var interv = function(w, t){ return function(){ if(typeof t === "undefined" || t-- > 0){ setTimeout(interv, w); try{ func.call(null); } catch(e){ t = 0; throw e.toString(); } } }; }(wait, times); setTimeout(interv, wait); };
Usage: This replaces all of the setIntervals but the waves do not look random.
interval(function(){ peak_wave( Math.floor((Math.random() * (30) + 1)) , Math.floor((Math.random() * (200) + 150))); peak_wave( Math.floor((Math.random() * (30) + 1)) , Math.floor((Math.random() * (200) + 150))); },1200, 999);
Problem: The code is a resource hog. I am looking for a way to optimize JavaScript (if JavaScript is the issue.)
Research: Looked for ways to reduce the number of set intervals I am using.
What I’ve tried: Setup a loop that creates the set intervals but I ran into the Issue where javascript blows through all the setintervals and they all start at the time time and fire at once. That is a HUGE resource issue.
Inspiration: YouTube: Link to video: https://www.youtube.com/watch?v=-475XHUgkiQ&t=22782s&ab_channel=APMusic
My Code: CodePen.io: Link to codepen.io: https://codepen.io/ac0ua/pen/BaReZEx
Thank you. Any help would be appreciated. I want to learn.
"use strict"; function peak_wave(wave_id, wave_height) { //console.log(wave_height); var wave = document.getElementsByClassName('w' + wave_id); var wave_height_buffer = wave_height; var a = 0; var i = 0; var counter = 0; var color = "white"; const waveLeftSide_arr = range(wave_id - 10, 10).reverse(); const waveRightSide_arr = range(wave_id + 1, 10); if (wave) { wave[0].style.height = wave_height + "px"; wave[0].style.background = color; wave[0].classList.toggle("l-active"); wave[1].style.height = wave_height + "px"; wave[1].style.background = color; wave[1].classList.toggle("r-active"); } var firstWaveClass = ""; for (let i of waveLeftSide_arr) { if (i <= -1) { i = 0 } counter = counter + 5 wave_height = wave_height - 10 - counter; const targetElement = document.querySelector('.w' + i); if (targetElement) { // var color = "yellowgreen"; document.getElementsByClassName('w' + i)[0].style.height = (wave_height) + "px"; document.getElementsByClassName('w' + i)[0].style.background = color; document.getElementsByClassName('w' + i)[1].style.height = (wave_height) + "px"; document.getElementsByClassName('w' + i)[1].style.background = color; } } wave_height = wave_height_buffer; counter = 0 for (let a of waveRightSide_arr) { if (a <= -1) { a = 0 } counter = counter + 5 wave_height = wave_height - 10 - counter; const targetElement = document.querySelector('.w' + a); if (targetElement) { if (firstWaveClass == "") { firstWaveClass = "w" + a; } // var color = "deeppinnk"; document.getElementsByClassName('w' + a)[0].style.height = (wave_height) + "px"; document.getElementsByClassName('w' + a)[0].style.background = color; document.getElementsByClassName('w' + a)[1].style.height = (wave_height) + "px"; document.getElementsByClassName('w' + a)[1].style.background = color; } } } function doesExist(class_name) { var element = document.getElementsByClassName(class_name) if (element > 0) { return true; } else { return false; } } setInterval(function() { var els = document.getElementsByClassName("wave"); for (var i = 0; i < els.length; i++) { // var NewHeight = els[i].offsetHeight - 10; // if(NewHeight <= 0){NewHeight = 0 } els[i].style.height = 0 + "px"; els[i].style.background = "white"; els[i].classList.remove("l-active"); els[i].classList.remove("r-active"); } }, 500); var circle = document.getElementsByClassName("circle")[0]; setInterval(function() { circle.style.transform = 'rotate(' + Math.floor((Math.random() * (180) + 1)) + 'deg)'; }, 120); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))); }, 1200); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))) }, 1300); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))) }, 1400); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))) }, 1500); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))) }, 100600); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))) }, 100700); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))) }, 100800); setInterval(function() { peak_wave(Math.floor((Math.random() * (30) + 1)), Math.floor((Math.random() * (200) + 150))) }, 100900); //peak_wave(50, 300); function range(start, count) { if (arguments.length == 1) { count = start; start = 0; } var foo = []; for (var i = 0; i < count; i++) { foo.push(start + i); } return foo; }
/* default template -- enable SCSS*/ html, body { width: 100%; height: 100%; margin: 0; padding: 0; background: #424242; color: white; font-family: bungee; display: flex; flex-direction: column; } html:before, body:before { content: " "; position: absolute; top: -40px; left: -60px; width: 50%; height: 400px; border-radius: 50%; background: linear-gradient(to bottom right, deepskyblue 20%, deepskyblue 50%, white 80%); filter: blur(80px); mix-blend-mode: overlay; } html header, body header { width: 100%; display: grid; place-items: center; } html section, body section { width: 100%; display: grid; place-items: center; } html section #wrapper, body section #wrapper { display: flex; place-items: center; flex-wrap: nowrap; justify-content: center; height: 200px; width: 80%; position: relative; margin-inline: 20px; } html section #wrapper:after, body section #wrapper:after { content: ""; position: absolute; background: white; background: radial-gradient(circle, white 26%, rgba(2, 0, 36, 0) 95%); height: 3px; width: 100%; } html section #wrapper #left-wrapper, body section #wrapper #left-wrapper { display: flex; flex-wrap: nowrap; justify-content: space-around; align-items: center; width: 45%; height: 100px; } html section #wrapper #left-wrapper span, body section #wrapper #left-wrapper span { display: inline-block; width: 5px; background: white; transition: height 0.3s cubic-bezier(0.83, 1.5, 0.63, -0.71); } html section #wrapper #right-wrapper, body section #wrapper #right-wrapper { display: flex; flex-wrap: nowrap; justify-content: space-around; align-items: center; width: 45%; height: 100px; } html section #wrapper #right-wrapper span, body section #wrapper #right-wrapper span { display: inline-block; min-width: 2px; background: white; transition: height 0.3s cubic-bezier(0.83, 1.5, 0.63, -0.71); } html section #wrapper .l-active, body section #wrapper .l-active { position: relative; } html section #wrapper .l-active:before, body section #wrapper .l-active:before { content: ""; margin-left: -100px; position: absolute; top: 50%; transform: translateY(-50%); height: 60%; width: 100px; border-top-left-radius: 25% 10%; border-bottom-left-radius: 25% 10%; background: radial-gradient(circle at 100% 53%, yellow 15%, #00bcd4 34%, rgba(238, 130, 238, 0) 100%); filter: blur(10px); z-index: 1; mix-blend-mode: overlay; transistion: 0.3s ease; } html section #wrapper .l-active:after, body section #wrapper .l-active:after { content: ""; margin-left: -250px; position: absolute; top: 50%; transform: translateY(-50%); height: 30%; width: 200px; border-top-left-radius: 25% 10%; border-bottom-left-radius: 25% 10%; background: radial-gradient(circle at 100% 53%, #ff00ef 15%, #005cd4 34%, rgba(238, 130, 238, 0) 100%); filter: blur(20px); z-index: 1; mix-blend-mode: overlay; transistion: 0.4s ease; } html section #wrapper .r-active, body section #wrapper .r-active { position: relative; } html section #wrapper .r-active:before, body section #wrapper .r-active:before { content: ""; margin-right: 100px; position: absolute; top: 50%; transform: translateY(-50%); height: 60%; width: 100px; border-top-left-radius: 25% 10%; border-bottom-left-radius: 25% 10%; background: radial-gradient(circle at 0% 53%, yellow 34%, #00bcd4 15%, rgba(238, 130, 238, 0) 100%); filter: blur(10px); z-index: 1; mix-blend-mode: overlay; transistion: 0.3s ease; } html section #wrapper .r-active:after, body section #wrapper .r-active:after { content: ""; margin-right: 250px; position: absolute; top: 50%; transform: translateY(-50%); height: 30%; width: 200px; border-top-left-radius: 25% 10%; border-bottom-left-radius: 25% 10%; background: radial-gradient(circle at 0% 53%, #ff00ef 34%, #005cd4 15%, rgba(238, 130, 238, 0) 100%); filter: blur(20px); z-index: 1; mix-blend-mode: overlay; transistion: 0.4s ease; } html section #wrapper .circle, body section #wrapper .circle { box-sizing: border-box; width: 100px; aspect-ratio: 1/1; border-radius: 50%; border: 10px solid white; position: relative; z-index: 2; display: grid; place-items: center; background: conic-gradient(from 90deg at 50% 50%, #ffff00 2%, #00bcd4 7%, #ee82ee 21%, #ffffff 26%, #ffffff 41%, #ffffff 49%, #fffc00 51%, #00bcd4 64%, #ee82ee 72%, #ffffff 77%, #ffffff 100%); transform: rotate(10deg); transition: all 0.2s cubic-bezier(0.83, 1.5, 0.63, -0.71); } html section #wrapper .circle .outter-record, body section #wrapper .circle .outter-record { box-sizing: border-box; width: 65px; height: 65px; border-radius: 50%; border: 5px solid white; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: grid; place-items: center; background: conic-gradient(from 90deg at 50% 50%, #ffff00 2%, #00bcd4 7%, #ee82ee 21%, #ffffff 26%, #ffffff 41%, #ffffff 49%, #fffc00 51%, #00bcd4 64%, #ee82ee 72%, #ffffff 77%, #ffffff 100%); } html section #wrapper .circle .outter-record .inner-record, body section #wrapper .circle .outter-record .inner-record { background: white; box-sizing: border-box; width: 45px; height: 45px; border-radius: 50%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: grid; place-items: center; } html footer, body footer { width: 100%; display: grid; place-items: center; } html h1, body h1 { color: deepskyblue; } html h1 span, body h1 span { color: yellowgreen; } html h1 font, body h1 font { color: yellowgreen; font-family: Montserrat; font-weight: 700; } html h2, body h2 { color: deepskyblue; font-family: Montserrat; } html h2 span, body h2 span { color: yellowgreen; } html h2 font, body h2 font { color: yellowgreen; font-weight: 700; } /* default template end */ .wave { background: deepskyblue; }
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Bungee&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@100;700&display=swap" rel="stylesheet"> <header> <h1>Audio <span>Candy</span></h1> </header> <Section> <h2>CSS<span> / </span>Javascript <font> Animation</font> </h2> <div id="wrapper"> <div id="left-wrapper"> <span class="wave w50"> </span> <span class="wave w49"> </span> <span class="wave w48"> </span> <span class="wave w47"> </span> <span class="wave w46"> </span> <span class="wave w45"> </span> <span class="wave w44"> </span> <span class="wave w43"> </span> <span class="wave w42"> </span> <span class="wave w41"> </span> <span class="wave w40"> </span> <span class="wave w39"> </span> <span class="wave w38"> </span> <span class="wave w37"> </span> <span class="wave w36"> </span> <span class="wave w35"> </span> <span class="wave w34"> </span> <span class="wave w33"> </span> <span class="wave w32"> </span> <span class="wave w31"> </span> <span class="wave w30"> </span> <span class="wave w29"> </span> <span class="wave w28"> </span> <span class="wave w27"> </span> <span class="wave w26"> </span> <span class="wave w25"> </span> <span class="wave w24"> </span> <span class="wave w23"> </span> <span class="wave w22"> </span> <span class="wave w21"> </span> <span class="wave w20"> </span> <span class="wave w19"> </span> <span class="wave w18"> </span> <span class="wave w17"> </span> <span class="wave w16"> </span> <span class="wave w15"> </span> <span class="wave w14"> </span> <span class="wave w13"> </span> <span class="wave w12"> </span> <span class="wave w11"> </span> <span class="wave w10"> </span> <span class="wave w9"> </span> <span class="wave w8"> </span> <span class="wave w7"> </span> <span class="wave w6"> </span> <span class="wave w5"> </span> <span class="wave w4"> </span> <span class="wave w3"> </span> <span class="wave w2"> </span> <span class="wave w1"> </span> </div> <div class="circle"> <div class="outter-record"> <div class="inner-record"> </div> </div> </div> <div id="right-wrapper"> <span class="wave w1"> </span> <span class="wave w2"> </span> <span class="wave w3"> </span> <span class="wave w4"> </span> <span class="wave w5"> </span> <span class="wave w6"> </span> <span class="wave w7"> </span> <span class="wave w8"> </span> <span class="wave w9"> </span> <span class="wave w10"> </span> <span class="wave w11"> </span> <span class="wave w12"> </span> <span class="wave w13"> </span> <span class="wave w14"> </span> <span class="wave w15"> </span> <span class="wave w16"> </span> <span class="wave w17"> </span> <span class="wave w18"> </span> <span class="wave w19"> </span> <span class="wave w20"> </span> <span class="wave w21"> </span> <span class="wave w22"> </span> <span class="wave w23"> </span> <span class="wave w24"> </span> <span class="wave w25"> </span> <span class="wave w26"> </span> <span class="wave w27"> </span> <span class="wave w28"> </span> <span class="wave w29"> </span> <span class="wave w30"> </span> <span class="wave w31"> </span> <span class="wave w32"> </span> <span class="wave w33"> </span> <span class="wave w34"> </span> <span class="wave w35"> </span> <span class="wave w36"> </span> <span class="wave w37"> </span> <span class="wave w38"> </span> <span class="wave w39"> </span> <span class="wave w40"> </span> <span class="wave w41"> </span> <span class="wave w42"> </span> <span class="wave w43"> </span> <span class="wave w44"> </span> <span class="wave w45"> </span> <span class="wave w46"> </span> <span class="wave w47"> </span> <span class="wave w48"> </span> <span class="wave w49"> </span> <span class="wave w50"> </span> </div> </div> </Section> <footer>James learning </footer>