Answer the question
In order to leave comments, you need to log in
Is it possible to recurse in js via setTimeout?
As an addiction, I write a function:
function userFadeout2(el, delay, opacity) {
delay = typeof delay !== 'undefined' ? delay : 50;
if(el.style.opacity == "") {
el.style.opacity = 1;
opacity = el.style.opacity;
}
else if(typeof opacity !== 'undefined') {
el.style.opacity = opacity;
}
else {
opacity = el.style.opacity;
}
if(opacity == 0) {
el.style.display = 'none';
console.log('конец');
return true;
}
else {
setTimeout(function() {
opacity -= 0.1;
console.log(opacity);
return userFadeout2(el,delay,opacity.toFixed(1));
},delay);
}
}
function userFadeout(el, delay) {
if(el.style.opacity == "") {
el.style.opacity = 1;
}
if(el.style.opacity == 0) {
el.style.display = 'none';
return true;
}
else if((el.style.opacity -= 0.1) == 0) {
el.style.display = 'none';
console.log('конец');
return true;
}
else {
if(typeof delay != 'undefined' || delay == '') {
setTimeout(userFadeout, delay, el, delay);
}
else {
console.log(el.style.opacity);
setTimeout(userFadeout, 50, el);
}
}
}
undefined
0.9
0.8
0.7000000000000001
0.6
0.5
0.4
0.30000000000000004
0.19999999999999998
0.1
0
конец
Answer the question
In order to leave comments, you need to log in
Throwing away all the excess
if(opacity == 0) {
...
return true;
} else {
...
}
Everything is correct, it will return undefined. I think you are misunderstanding the execution of setTimout(). Here in this area
else {
setTimeout(function() {
opacity -= 0.1;
console.log(opacity);
return userFadeout2(el,delay,opacity.toFixed(1));
},delay);
}
else {
$.when(setTimeout(function() {
console.log("pause");
},delay)
).then(function(){
opacity -= 0.1;
console.log(opacity);
userFadeout2(el,delay,opacity.toFixed(1));
});
}
The function will only return true. Where are you testing the feature in the devtool? The console displays correctly for you. Your function returns undefined on the first call. This is when you launched the function for the first time in the console.
Then the console displays the console logs for you. On the second and so on iterations, the console will no longer display undefined.
Apparently you are testing it in the developer
console. Therefore, such consoles are logs. Your function doesn't return opacity anywhere.
Did you check what you posted in the console?
More or less like this
function userFadeout2(el, delay, opacity) {
return new Promise((resolve, reject) => {
delay = typeof delay !== 'undefined' ? delay : 50;
if (el.style.opacity == "") {
el.style.opacity = 1;
opacity = el.style.opacity;
}
else if (typeof opacity !== 'undefined') {
el.style.opacity = opacity;
}
else {
opacity = el.style.opacity;
}
if (opacity == 0) {
el.style.display = 'none';
console.log('конец');
return resolve(true)
}
else {
setTimeout(function () {
opacity -= 0.1;
console.log(opacity);
return resolve(userFadeout2(el, delay, opacity.toFixed(1)));
}, delay);
}
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question