If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Trouble Passing Variables to Function (Called by setTimeout)

Okay so I am having the following problem. I have gotten AJAX to work and am trying to get Javascript to fade out an old review score and fade in with a new score. The important part right now is that when I try to run it I get the following error:

There are some functions defined outside of this function but I don't think those are necessary for me to post right now. The issue seems to be that I'm not sure how to properly send a variable to a function when that function is called via setTimeout. Any help would be appreciated. Thank you.

setTimeout expects a function as the first parameter so the string you supply is effectively being wrapped in an anonymous function that is run after the specified time.

The preciseResponseAverage variable is only defined in the anonymous function attached to the onreadystatechange event handler and so that copy has gone out of scope by the time that your string gets converted into a function.

Converting it to a function yourself so that the fields it references are visible before they go out of scope will hopefully create the needed closure.

Yeah I decided the best way to do it was to modify and use a setTimeout function within a for loop. For some reason it seems like it is not going through the loop correctly though. Here is the relevant part of my code:

I was hoping for a slow fade but am not getting that. Currently it's not even changing the opacity at all when I click on it, nor the color of the text. In fact I added the logo.style.width = 500 line specifically to test to see whether it's even looping at all and it appears that it's not because the logo on the page is not changing to 500px (whether I include the 'px' or not). It's will change the actual rating itself though...

Edit: Heck I even got it to change the color but when it changes there is no opacity fade at all. It's like Javascript is only executing the final part of the loop or something...

You see that?
The first call to fade will be at 100*(0+1) milliseconds.
The second call to fade will be at 100*(1+1) milliseconds.
...
The 10th call to fade will be at 100*(1+9) milliseconds, or 1 full second later.

Is this the best way to do it? Probably not. But it's workable.

Last edited by Old Pedant; 01-24-2013 at 08:29 AM.

An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

And, yes, you *MUST* specify "px" when changing a style property if you want the number to be interpreted as pixels. MSIE will default to pixels, but other browsers will just ingore the number. Remember, you can use "pt" and "%" and a few other things other than "px", so the browser insists you tell it which.

An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.