Int*_*Man 3 html javascript recursion settimeout
这是我尝试编写动态onmouseout事件,当鼠标离开div时缓慢改变不透明度.由于某种原因,递归和超时似乎没有工作属性,并且不透明度的变化立即完成.
问题: 是否有任何原因setTimeout()
不适用于递归?有没有更好的方法来解决这个问题?
function hide(id)
{
if (gOpacity > .4)
{
gOpacity -= .1;
document.getElementById(id).style.opacity = gOpacity;
setTimeout(hide(id),1000)
}
else
{
gOpacity = 1.0
}
}
Run Code Online (Sandbox Code Playgroud)
Bru*_*oLM 11
将setTimeout调用更改为
setTimeout(function() { hide(id); } ,1000)
Run Code Online (Sandbox Code Playgroud)
所以它将在1s后执行,而不是立即执行