Javascript Recursive Timeout Call
This is my attempt at writing a dynamic onmouseout event that slowly changes the opacity when the mouse leaves the div. For some reason the recursion and timeout seem to be no wor
Solution 1:
Change your setTimeout call to
setTimeout(function() { hide(id); } ,1000)
So it will execute after 1s, and not immediately
Solution 2:
Have you tried this?
functionhide(id)
{
if (gOpacity > .4)
{
gOpacity -= .1;
document.getElementById(id).style.opacity = gOpacity;
setTimeout(function() {
hide(id);
},1000)
}
else
{
gOpacity = 1.0
}
}
Solution 3:
i thinck that when you type :
setTimeout(hide(id),1000);
what you really mean is :
setTimeout(hide.bind(this,id),1000);
becasue in the first case, the function will be call instantly when setTimeout is call - it is an argument of setTimeout- . In the second case, this will be a bound function. So yes this and id are evaluated, but the function is not called until the 1000 ms elapsed.
(this is just a faster to run / faster to type way of creating a function ).
Solution 4:
wrap all your code in recursive function by setTimeout
functionhide(id)
{
setTimeout(function() {
if (gOpacity > .4)
{
gOpacity -= .1;
document.getElementById(id).style.opacity = gOpacity;
hide(id);
}
else
{
gOpacity = 1.0
}
},1000)
}
Post a Comment for "Javascript Recursive Timeout Call"