Hello fellow web developers!
In connection with an ajax-game I’m creating for the moment, I have been running into a problem using setTimeout().
Even though I call setTimeout from inside af member of an object it uses global variables. This is very annoying! ?
The problem occurs for example when I’m using my “fader-function” (a function that fades html-elements in/out) on several elements at a time:
[code=html]for(i=1; 5>=i; i++) {
opacity = (fade_to == ‘1’) ? i/5 : 1-i/5;
setTimeout(“document.getElementById(‘” + obj_id + “‘).style.opacity = “+opacity, (i-1)*40);
}
The opacity-var and the obj_id var will be affected from other instances of the function if to faders are running simultaenously. This results in weird results – the elements are blinking instead fading nice and slow.
Am I using the setTimeout-function in a stupid way or what? What is the easiest way out of this? Suggestions are very welcome.
Thank you,
Rasmus