spiderx
Tuesday, September 01, 2009
  pausing for input - javascript
I write websites and connect them to databases as part of my job, and I've found this little trick helps out quite a bit when I'm writing some javascript which tries to autofill or search while typing.

function KeyHandler()
{
if(!(typeof nagle == "undefined")){clearTimeout(nagle);} // if there's a timeout set on SendKeys, clear it...
nagle = setTimeout("SendKeys()",250) // set new timeout to 250ms....
return false;
}


That little bit of script creates a function which can be called on every keypress event, and after the last key is pressed, it will wait 1/4 of a second before running the function "sendkeys".
It doesn't have to be related to sending data to a server though, you can use this with other scripts, like if you want to filter a table or something else.

Oh, and I chose the word 'Nagle' because it was part of a larger script which implemented John Nagle's idea of waiting a moment before data is sent, with the intention of reducing the overhead of single keypresses. His idea was to reduce the amount of data sent over a network, and my idea is to reduce the number of queries to my server.
 
Comments: Post a Comment

Links to this post:

Create a Link



<< Home
best of the web (sorta)

My Photo
Name:
Location: Phoenix, Arizona, United States
ARCHIVES
August 2003 / January 2004 / April 2004 / May 2004 / December 2004 / January 2005 / February 2005 / March 2005 / April 2005 / May 2005 / June 2005 / July 2005 / August 2005 / September 2005 / October 2005 / November 2005 / December 2005 / January 2006 / February 2006 / March 2006 / April 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / April 2007 / July 2007 / August 2007 / September 2007 / October 2007 / November 2007 / December 2007 / January 2008 / March 2008 / April 2008 / June 2008 / July 2008 / August 2008 / September 2008 / October 2008 / November 2008 / December 2008 / January 2009 / February 2009 / March 2009 / June 2009 / July 2009 / September 2009 / October 2009 / November 2009 / October 2010 / March 2011 / June 2011 /


Powered by Blogger