So what I thought was a simple and common goal of being able to grow and shrink the height of a div/page turned out to be the opposite, which makes me question if the method I am using is correct.
I have a div that takes up 100% width and height, and I want some type of animated welcome page of some sorts.
So I tried using a simple js function:
for(var i = 0; i<=100; i++){
setInterval(
function(){
var test =$('#bg0');
test = test.css("height", i + "%");
}, 1000
);
}
I have no idea why this doesn’t work, I assumed that this use case is the entire purpose of the setInterval function.
The most concerning part for me is this… I found the solution but I almost wish I didn’t. I have no idea how this works and it’s kind of melting my mind.
The article I ripped the code from says it uses an Immediate Invoking Function Expression(IIFE) but that’s pretty much the extent of the explanation
The first perplexing part for me is obviously using i as an argument in the IIFE function, what is this actually doing? Creating a function for every iteration?
But the most mind melting part is the i variable being multiplied by the timeout duration at the end of the anon function … wouldn’t that result in an exponentially increased time?
This code seems like black magic, I would greatly appreciate anyone’s attempt in elucidating this for me.
Thanks in advance
for(var i = 0; i<=100; i++){
(function(i){
setTimeout(
function(){
var test =$('#bg0');
test = test.css("height", i + "%");
}, 10*i
);
})(i);
}