www.webdeveloper.com
Results 1 to 11 of 11

Thread: dynamically create and delete a function at runtime

Threaded View

  1. #1
    Join Date
    Jan 2008
    Posts
    9

    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles