You could combine a keypress event handler with setTimeout so that you send an Ajax request a set amount of time after a keypress, cancelling and restarting the timer if another keypress occurs before the timer finishes. Assuming you have a textarea with id 'myTextArea' and an Ajax callback function called doAjaxStuff : function addTextAreaCallback(textArea, callback, delay) { var timer = null; textArea. Onkeypress = function() { if (timer) { window.
ClearTimeout(timer); } timer = window. SetTimeout( function() { timer = null; callback(); }, delay ); }; textArea = null; } addTextAreaCallback( document. GetElementById("myTextArea"), doAjaxStuff, 1000 ).
You could combine a keypress event handler with setTimeout so that you send an Ajax request a set amount of time after a keypress, cancelling and restarting the timer if another keypress occurs before the timer finishes. Assuming you have a textarea with id 'myTextArea' and an Ajax callback function called doAjaxStuff: function addTextAreaCallback(textArea, callback, delay) { var timer = null; textArea. Onkeypress = function() { if (timer) { window.
ClearTimeout(timer); } timer = window. SetTimeout( function() { timer = null; callback(); }, delay ); }; textArea = null; } addTextAreaCallback( document. GetElementById("myTextArea"), doAjaxStuff, 1000 ).
This is the generally accepted "right" way to do this. Well said. – Gabriel Hurley Oct 25 '09 at 21:09 Thanks Tim, brilliant solution :D – Bruno De Barros Oct 27 '09 at 0:44 use onkeyup if you want to be able to capture backspace key events.
– barkmadley Oct 27 '09 at 4:26 @barkmadley: that's fine if you don't want to be able to prevent its default action. You can capture backspace key events reliably in a keypress handler and prevent the default action. – Tim Down Oct 27 '09 at 9:16.
Another alternative is to use a small jQuery plugin called bindWithDelay. It uses the same setTimeout technique that the accepted answer has, but handles the timeouts transparently so your code is a little easier to read. The source code can be seen on github.
$("#myTextArea"). BindWithDelay("keypress", doAjaxStuff, 1000).
I would personally use a setInterval that would monitor the textarea for changes and only perform AJAX callbacks when it detects a change is made. Every second should be fast and slow enough.
This requires setInterval to run forever. Tim's solution is much more elegant, only running when someone is actually interacting with the textbox. – Gabriel Hurley Oct 25 '09 at 21:10 run forever until the page is unloaded.
Also if you would like to provide some feedback as the user is typing my solution is better since it has the chance to send a request if the user is typing faster than one key per second. I'm not saying Tim's solution isn't elegant, but that in some cases it is not the solution depending on the application. – barkmadley Oct 26 '09 at 8:11.
If you're interested in jQuery solutions, jQuery Suggest is a good implementation. No sense reinventing the wheel every time.
This doesn't work for me, as I don't want to update the textarea or anything with the suggestions made. I just want to make an AJAX request to get the contents of another element in the page, every X keystrokes, or every X seconds the user is typing, or something. Just wondering what would be the most efficient option.
– Bruno De Barros Oct 25 '09 at 11:22.
If you are using this functionality alot around the site and don't want to keep track of all of the setTimeout refs etc then I packaged this up into a plugin available here. The plugin adds a few options to the normal $. Ajax method for buffering and canceling previous ajax calls.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.