Created
September 1, 2020 12:40
-
-
Save dtaivpp/2dcfd2860c33efdeb28ea3ac7d30b063 to your computer and use it in GitHub Desktop.
A simple example of how you can slowly change the opacity of an html element using a promise chain.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// html has some element with the id heading | |
// <h1 id="heading">IM FADING IN</h1> | |
window.onload = () =>{ | |
heading = document.getElementById("heading"); | |
create_await_chain(heading); | |
} | |
function create_await_chain(fade_object, depth=0){ | |
// Creates a promise chain to slowly load the bar | |
if (depth < 100){ | |
return new Promise((resolve, reject)=>{ | |
setTimeout(() => { | |
fade_object.style.opacity = depth / 100); | |
} | |
resolve(); | |
}, depth * 30); | |
}).then(create_await_chain(fade_object, depth+1)) | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment