This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setInterval() - takes function with argument?

 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to pass an argument to a function that you are passing into setInterval()?



I couldn't seem to get the following working. The kludge I came up with was to make a variable global (well, at least within the object), and then just pass



Am I missing something obvious, or is there in fact a restriction on passing objects through the setInterval() function?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not actually passing it a function.
You're passing it a string which gets evaled into Javascript code and then run.

So, something like this would work (it's ugly but it shows the point):


[ November 26, 2007: Message edited by: Ben Souther ]
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ben.

So it will only recognize the parameter if it is defined within the String that you are sending in?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly the type of thing closures are meant for. With closures you can bind local variables to function instances so that they are available even outside the scope where the variable is declared.

This eliminates the need to pollute the global namespace with variables.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear - I never would have figured out what to feed into google! Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. In my opinion, closures are one of the most important concepts to understand to write effective JavaScript.
[ November 26, 2007: Message edited by: Bear Bibeault ]
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to get a simple closure working:



And I think I am starting to get a hazy idea of what is going on.

I wasn't able to do the following:


So I could access the local variable 'color', but I couldn't do anything to the local variable 'o'.

What is the difference?

And then... how would I pass in the color like this:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't exactly able to tell what you are trying to do, but in this snippet:

Why o? Why not just perform the element lookup within the swap function?
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just experimenting.

I couldn't figure out how to pass a value into the function and then act on it.

Needless to say, my brain was fried after 13 hours of programming, and all I needed was a few hours of sleep.



I was sure that I had tried this yesterday and failed.

The next experiment is to pass an object in to the function and then act on that. We'll see.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Katrina Owen:
my brain was fried after 13 hours of programming

Haven't you learned not to do that yet? The Law of Diminishing Returns and all that...

The next experiment is to pass an object in to the function and then act on that. We'll see.

If your code is not the code that is calling the callback function you have no control over what gets passed to the invocation of the function. Closures are the only way you'll be able to supply data to a callback function that you do not call yourself. (Well, there's the whole concept of function context pre-binding but that's way way way out there and probably not something that we need to get into).
[ November 27, 2007: Message edited by: Bear Bibeault ]
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Haven't you learned not to do that yet? The Law of Diminishing Returns and all that...


I know, I know, but I get so hooked into it that I can't put it away.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it!


Thanks for the help (and the reality checks )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic