Slightly offtopic, but I thought of a simple way you could lessen the load on your server.
You mentioned in your explaination that you were using the onkeyup event to send the requests. A way of stopping the /. weenies from borking your server would be to change the code in the code to the following:
var idTimer = null;
function SendQuery(key) { var url = "http://www.objectgraph.com/dictionary/dict.aspx?k=" + key;
// Stop the previous timer (if any) from firing. if (idTimer != null) window.clearTimeout(idTimer);
// This code is ran if a key hasn't been pressed for quarter a second. var handler = function() { Initialize();
// Set the handler to run after a quarter a second. idTimer = window.setTimeout(handler, 250); }
I wish Blogger allowed preformatted blocks. :-(
This makes it wait until the user has stopped typing for a reasonable period (250ms) before firing back to the server to pull stuff from the dictionary.
Note that I just typed this up off the top of my head and haven't tested it, so you should make sure it works before you put it online.
the only problem i have is, if the person just leaves the computer idle with some text already typed in the box, wouldnt that keep on hitting the server?
No, once the event fires, it should disappear. Timeout isn't for recurring events, just once-offs. Even then, you could cancel it once it fires even if it did.
5 Comments:
Slightly offtopic, but I thought of a simple way you could lessen the load on your server.
You mentioned in your explaination that you were using the onkeyup event to send the requests. A way of stopping the /. weenies from borking your server would be to change the code in the code to the following:
var idTimer = null;
function SendQuery(key)
{
var url = "http://www.objectgraph.com/dictionary/dict.aspx?k=" + key;
// Stop the previous timer (if any) from firing.
if (idTimer != null)
window.clearTimeout(idTimer);
// This code is ran if a key hasn't been pressed for quarter a second.
var handler = function()
{
Initialize();
if (req != null)
{
req.onreadystatechange = Process;
req.open("GET", url, true);
req.send(null);
}
};
// Set the handler to run after a quarter a second.
idTimer = window.setTimeout(handler, 250);
}
I wish Blogger allowed preformatted blocks. :-(
This makes it wait until the user has stopped typing for a reasonable period (250ms) before firing back to the server to pull stuff from the dictionary.
Note that I just typed this up off the top of my head and haven't tested it, so you should make sure it works before you put it online.
By Keith Gaughan, at 8:12 PM
Cool Idea,
the only problem i have is, if the person just leaves the computer idle with some text already typed in the box, wouldnt that keep on hitting the server?
By Anonymous, at 6:54 AM
No, once the event fires, it should disappear. Timeout isn't for recurring events, just once-offs. Even then, you could cancel it once it fires even if it did.
By Keith Gaughan, at 7:53 AM
Cool,
I will implement this and post the source code.
Thank you for the suggestion.
By gavi, at 9:47 PM
I really like what you've done here. I will probably dream tonight as I sleep about this program hooking up with onelook.com
By Anonymous, at 10:51 PM
Post a Comment
<< Home