I’m trying to create a jquery fadeto type of effect in Javascript, but am having an issue with my setTimeout command.
Here is the code:
function textfade(y) {
var x = document.getElementById("test");
var y;
if (y == undefined) {
y = 1.0;
}
x.style.opacity = y;
y -=0.1;
setTimeout(function(){ textfade(y); }, 50);
}
The problem is x.style.opacity = y.
Without that, the timeout runs fine. With it, however, it runs through the function one time and then dies. While I feel like it’s a simple error, I am out of ideas for fixing it.
Any advice would be greatly appreciated.
Thank you in advance.
Ensure that you are running it after
testelement is already available. Here it works fine: http://jsfiddle.net/3yDMP/ . And here: http://jsfiddle.net/3yDMP/3/ – no , because function is called in head, not in onload (like in first fiddle), when dom is not ready yet and is not available.So, in your could be