dynamically create and delete a function at runtime
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:
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?
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
Last edited by devboy; 07-01-2010 at 03:56 AM.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread