dynamically create and delete a function at runtime
Hi,
I'm using a 3rd party API that does asynchronous requests for me and directs response to a callback that I specify. Since my code can issue multiple requests concurrently I want some mechanism to match each response to the specific request e.g. attach a unique ID to be returned with the response. The 3rd party API does not allow me to attach any user context to the request so the only way I could figure out how to do this was to dynamically generate a callback function per request and embed the unique id in the body of that function. When the callback is invoked I can use the information to complete my handling of the response.
1) First of all if anybody has a better idea on how to achieve the above without dynamically generating script I'd be happy to hear it.
2) Regardless of (1), if using my proposed method repeatedly I will be generating a lot of garbage over time, so I want to clean up after myself. Meaning, to delete the dynamic callback after it completes. Below is an example of how I wanted to do it, but it seems not to work:
Code:
function CreateDynamicCallback(callID)
{
var it = eval('callback_' + callID + " = function (response)" +
"{" +
" alert(eval('fql_callback_' + callID));" +
" var i = delete eval('fql_callback_' + callID);" +
" alert(i);" + //returns true but does NOT delete function as seen in next instruction
" alert(eval('fql_callback_' + callID));" + //Still returns the eval of the function
"}");
return it;
}
It is possible the final alert still works since I am still inside the callstack for that function and that it will be collected later. Any ideas?
Cheers,
DB
Last edited by devboy; 07-01-2010 at 03:56 AM.
Reason: mistake
Not very clear what you want, but the functions are in fact properties/methods of the Global Window Object.
So: to create/change dynamically a function:
Done as you suggest. It works. One thing though, after nullifying the function, doing alert(myFunction); would still display the message "function () { }" meaning there IS such a function but bodiless. Was that your intention?
Well. what is your intention? Is it so important to you to alert the function? What's the use? A null function is a function which does nothing. Isn't it what you need?
I think that you can not simply destroy the JavaScript objects once they were created, whatever you might understand by that. You can nullify them or send them to the garbage collector, but this is another discussion: were or not the nullified object sent to the garbage in different browsers?
I missed your last post...
The alert was just to print out object evaluation.
My intention is this. I need to inject some user context to a 3rd party API call. An API call which does not allow for user context. Since the API call gets a callback function pointer from me at invocation, I chose to dynamically create that callback each time, thus allowing me to 'embed' the user context inside the callback function itself. To allow for multiple concurrent calls of this type, I had to give each of these dynamically generated callbacks a unique name. For a long living application this could cause an issue, where I have a lot of these 'garbage' callbacks that aren't being used. The point of the exercise was how to set this mechanism up so that the callback will actually delete itself once it's done. I'm not 100% sure yet, but it seems that I cannot delete the function from within it's own execution, but I can set a timer to delete each callback once that callback returns.
Hope this whole thing made more sense.
Regarding the theoretical question you posed... about garbage collection in different browsers. Was that rhetorical? I mean do you know the answer to that? Cuz as I said the whole point is to do proper garbage collection.
I'm not 100% sure yet, but it seems that I cannot delete the function from within it's own execution, but I can set a timer to delete each callback once that callback returns.
you should be able to; the window.XXX and the function itself are two separate things.
Bookmarks