more on JSONP

more on JSONP


Still haven't had time to look more closely at what it'd take to implement script timeouts specifically in jquery.
Another thing I've noticed is on a jsonp-style $.ajax call (in 1.2.1), the script tag never seems to get removed from head.  Perhaps this is fixed in svn? (haven't had time to check).
In case anyone else wants to dig on this before I have time to look into it, for my own project I've built a stand-alone function that only does jsonp and
it seems to work well.  It has a timeout callback and interval, and cleans up after itself (removing script tag, unsetting temp vars) pretty well. I borrowed some ideas from jQuery core.  I'm not "done" with this standalone version -- I might dive in to core to see what it would take to replicate this there or turn this into a standalone plugin that plays nice with jQuery Ajax Events.
Criticism, comments and feedback appreciated. 
// Does JSONP call with provided options:
//    url:                 URL to JSONP method, ending in ? which is replaced by generated callback function
//    onSuccess:     callback function on success
//    onTimeout:     callback function on timeout
//    timeout:         time in ms to wait for script to execute
var uniq = (new Date).getTime();  // For generating unique IDs for jsonp calls
var jsonp = new Array;            // Array to hold active jsonp calls
var JSONP = function(options) {
    var id = 'jsonp'+ uniq++;             // Generate unique id for this call
  jsonp[id] = { hasRun: false }; // Keep track of script run status
    // Define callback function for JSONP call
    window[ id ] = function(tmp) {
        jsonp[id].data = tmp;
        jsonp[id].hasRun = true;
        // cleanup
        window[ id ] = undefined;
        try{ delete window[ id ]; } catch(e){}
    }
   
    // Stick generated callback function on end of provided method URL
    options.url = options.url.replace(/=(\?|%3F)/g, "=" + id);
   
    // Attach script URL to head
    var head =
document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.src = options.url;
    head.appendChild(script);
    // Register callbacks & timeout interval value
  var onTimeout = options.onTimeout;
  var onSuccess = options.onSuccess;
  var timeout = options.timeout;
    // Poll for script completion on timeout interval
  var i = 0;
  jsonp[id].poll = setInterval(function(){
    // "success"
    if(jsonp[id].hasRun == true) {
      clearInterval(jsonp[id].poll);
      onSuccess(jsonp[id].data);
            // cleanup
            head.removeChild( script );
            jsonp[id] = undefined;
            try{ delete jsonp[id]; } catch(e){}
    }
    else {
      i += 13;
      // "timeout"
      if(i > timeout) {
        clearInterval(jsonp[id].poll);
        onTimeout(i);
                // cleanup
                head.removeChild( script );
                jsonp[id] = undefined;
                try{ delete jsonp[id]; } catch(e){}
      }
    }
  }, 13)
};