File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes setInterval() - takes function with argument? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "setInterval() - takes function with argument?" Watch "setInterval() - takes function with argument?" New topic
Author

setInterval() - takes function with argument?

Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1364
    
  17
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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1364
    
  17
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1364
    
  17
Bear - I never would have figured out what to feed into google! Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

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

Joined: Nov 03, 2006
Posts: 1364
    
  17
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

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

Joined: Nov 03, 2006
Posts: 1364
    
  17
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

Joined: Jan 10, 2002
Posts: 61437
    
  67

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

Joined: Nov 03, 2006
Posts: 1364
    
  17
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

Joined: Nov 03, 2006
Posts: 1364
    
  17
I got it!


Thanks for the help (and the reality checks )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setInterval() - takes function with argument?