I used this tutorial to create a basic Javascript function for implementing change during a given period of time.
function fadetext(){
if(hex>0) {
hex-=11;
document.getElementById("sample").style.color="rgb("+hex+","+hex+","+hex+")";
setTimeout("fadetext()",20);
}
else
hex=255
}
Question 1: How to enter variable via function; e.g. fadetext(element, time). I was unable to do so by simply replacing the values with variables.
Question 2: Is it the standard (and right) method to gradually implement a change by javascript? The confusion point for me is that we use fadetext() function inside its own function. Doesn’t this make overload?
You need to pass a function, not a string, to
setTimeout:This code passes an anonymous function which captures the original parameters in a closure.