javascript - How do I remove setInterval on a function? -
i've function called fadein
, want return until condition met.
i tried put alert in else line along clearinterval(myvar)
, keeps alerting.
css:
* { background-color: black; padding: 0; margin: 0; height: 100%; } #title { background-color: white; height: 50px; } audio { display: none; } #audio-icon { width: 50px; background-color: white; } #focus { background-color: black; margin: auto; width: 100%; height: 700px; border: 5px, solid, grey; border-style: inset; border-radius: 10px; } #text-box { background-color: black; width: 100%; height: 200px; margin-top: 500px; float: left; border: 5px, solid, grey; border-style: inset; border-radius: 10px; } #command-line { background-color: black; width: 100%; height: 50px; margin-top: 150px; float: left; border: 5px, solid, grey; border-style: inset; border-radius: 10px; } #element { opacity: 0.1; }
html:
<audio id="background-audio" controls autoplay="autoplay" loop> <source src="darkness.mp3" type="audio/mpeg"> </audio> <div id="title"> <img id="audio-icon" src="unmute.png" onclick="mute()"> <img id="element" src="123.png"> </div> <div id="focus"> <div id="text-box"> <div id="command-line"></div> </div> </div>
javascript:
function fadein() { var myvar = setinterval(fadein, 500); var element = document.getelementbyid('element'); if (element.style.opacity < 1) { element.style.opacity -= '-0.1'; } else { clearinterval(myvar); } } function mute() { var audio = document.getelementbyid('background-audio'); var img = document.getelementbyid('audio-icon'); if (audio.muted) { audio.muted = false; img.src = 'unmute.png'; } else { audio.muted = true; img.src = 'mute.png'; } } document.onload = fadein();
update:
i have put variables outside of function , of sudden code seems work correctly. not sure how possible since have tried already. code wouldn't run @ , alert
keep alerting. anyway help. first question apart few typos , way listed code don't know why downvoted feedback welcome! here current working code:
<script> var myvar = setinterval(fadein, 500); var element = document.getelementbyid('element'); function fadein() { console.log(element.style.opacity) if (element.style.opacity < 1) { element.style.opacity -= '-0.1'; } else { clearinterval(myvar); } } function mute() { var audio = document.getelementbyid('background-audio'); var img = document.getelementbyid('audio-icon'); if (audio.muted) { audio.muted = false; img.src = 'unmute.png'; } else { audio.muted = true; img.src = 'mute.png'; } } document.onload = fadein(); </script>
you call fadin func onload (that's fine) create queue execute function every half second, in each iteration create queue execute functio every half sec.... sooner or later you're going kill browser this.
what you're looking more so:
function fadein() { if (opacity < 1) { opacity += 0.1; settimeout(fadein, 500); } } document.onload = fadein();
if not purpose of learning javascript should things fadein / fadeout using css transitions rather javascript.
Comments
Post a Comment