Plugin writing

Plugin writing

Hi guys

I am writing a plugin to wrap some suggestive searching into an application.

I needed many suggestive's on one page so i decided to write a plugin to do it..

The plugin is going okay except I am a bit stuck with 1 line of the javascript.

This might be a bit off topic but i thought i would ask.

In short my suggestive uses timeouts to send the values to the server to get some text back... It basically sets a timeout on a keyup and cancels / resets on the next press... for example if you type a letter then another one within 600ms the first request is canceled and the second one starts the timeout loop again and so on (saves on mass server load).. I have this working properly except the timeouts is a global variable which i need to change into the scope of the plugin...

For  example in the global sense the following code works fine.



  1. var timeouts;

  2. if(timeouts) { clearTimeout(timeouts);  }
  3. //... my code to create the container etc etc (boring and not needed for this example)

  4.     timeouts=window.setTimeout(function() {
                $.post("/suggestive-search",{ query: string},
     function(data){
                     
                    $("#qs-results").html(data);
                    $("#qs-results").fadeIn(400);
    });
        },600); // end window settimeouts   







But what i want to do is something like this...

  1. var rand=new Date().getTime();
    var timeouts+'-'+rand;
    if(timeouts+''+rand) { clearTimeout(timeouts+''+rand);  }

  2. // boring code all working

            timeouts+''+rand=window.setTimeout(function() {
                $.post("/suggestive-search",{ query: text},
     function(data){
                     
                    $("#"+settings.ResultsId+"").html(data);
                    $("#"+settings.ResultsId+"").fadeIn(400);
    });
        },600); // end window settimeouts   













But i cannot seem to set the variable in timeouts - It's an IE thing as it needs the declaration.. Is it better to sit the timeouts variable as an array like var timeouts[rand]; then use that ? or is there a better way to set a dynamic variable with no attachment like above?

Kind regarsds