Steven Albarracin
2005-Dec-12 17:09 UTC
PeriodicalExecuter passing variables to the callback function
I trying to pass a variable to the call back function of when utilizing the PeriodicalExecuter function ex. new PeriodicalExecuter(refresh, 10); works but new PeriodicalExecuter(refresh(id), 10); works once then errors after the 10 seconds, I get "this.callback is not a function"
Steven Albarracin
2005-Dec-12 17:20 UTC
PeriodicalExecuter passing variables to the callback function
I trying to pass a variable to the call back function of when utilizing the PeriodicalExecuter function ex. new PeriodicalExecuter(refresh, 10); works but new PeriodicalExecuter(refresh(id), 10); works once then errors after the 10 seconds, I get "this.callback is not a function"
John Butler
2005-Dec-12 17:22 UTC
Re: PeriodicalExecuter passing variables to the callback function
The difference is that in the first example you are passing a reference to the function "variable". In the second example you are actually executing the function ( which is why it works once ) and the returned result of the "refresh()" function is then passed to PeriodicalExecuter. ( which is probably null, which is why it fails ). So the order of operations for your second example is: 1) Execute refresh function and pass it the current value of id. Get the returned result 2) Pass the returned result as the first argument of new PeriodicalExecuter 3) PeriodicalExecuter tries to execute the returned result from the refresh function (which is not a function object, so it complains ). To get around this you can create a new lambda function and pass that to PeriodicalExecuter. There are two ways to do this: 1) new PeriodicalExecuter( function(){refresh(id)}, 10 ); 2) new PeriodicalExecuter( new Function("refresh(''"+id+"'')"), 10 ); The "problem" with the first one is that when the PeriodicalExecuter gets called it will be called with whatever the CURRENT value of id is ( not the value of id at the time you created the PeriodicalExecuter ). This is probably not what you want. Which is you why you should use the second option which creates a new function dynamically ( basically it evals the string you pass into it ). So in the second example, if the current value of id was "elem1" it would create a function like: function() { refresh(''elem1''); } which should work for you. They are called lambda function because they don''t have any names. P.S. Here is a good reference on javascript functions in general: http://www.permadi.com/tutorial/jsFunc/ -John On Dec 12, 2005, at 9:09 AM, Steven Albarracin wrote:> I trying to pass a variable to the call back function of when > utilizing the PeriodicalExecuter function > > ex. > > new PeriodicalExecuter(refresh, 10); > works > > but > new PeriodicalExecuter(refresh(id), 10); > works once then errors after the 10 seconds, I get "this.callback > is not a function" > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Maybe Matching Threads
- Does prototype get rid of PeriodicalExecuter once it is stop()?
- PeriodicalExecuter stops when form is submitted in Safari 3
- Managing PeriodicalExecuters in an Ajax app. How?
- prototype PeriodicalExecuter pause/restart patch
- Turn off Responder for PeriodicalExecuter?