Answer the question
In order to leave comments, you need to log in
Why does this function work (closures in javascript)?
There is a code:
var div = document.querySelector('.test');
function hide(elem, t) {
var fadeEffect = setInterval(function () {
if (!elem.style.opacity) {
elem.style.opacity = 1;
}
if (elem.style.opacity < 0.02) {
clearInterval(fadeEffect);
elem.style.display = "none";
}
elem.style.opacity -= 0.02;
}, t)
}
div.onclick = function () {
hide(this, 19);
}
t
into the arguments of the function hide()
. Thank God of course that works, but I would like to figure it out! t
would not be able to reach the time argument in setInterval.
Answer the question
In order to leave comments, you need to log in
I don't understand anything. You pass the t parameter to hide. Inside hide, you use the t parameter as an argument to the setInterval function.
Because you declared that the function can have a parameter t and passed it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question