Fundamentally, you need to capture the keypress event and start a timer. If the timer runs out before another key is pressed, submit the form
Detecting key pressed with jQuery is done like this:
$('#ElementId').keypress(function() {
/* Your code */
});
You can use the setTimeout()
method to time 3 seconds (See here)
and finally, use jQuery's submit()
as shown here to submit the form
You may also want to use focus()
to move the focus to the text input once the page has loaded so that SR is "typing" in the right place,
Additionally, this article is a good intro to timing events in JS, including how to cancel them
In short, you want something like this (tested):
$(document).ready(function(){
var Timeout; //For reference to timeout
var DelayInMs=3000;
$("#SRInput").focus(); //Give focus to input on document ready
//When input is received, (re)set the timer
$("#SRInput").keypress(function() {
if(Timeout) {clearTimeout(Timeout);} //Clear existing timeout, if any
Timeout = setTimeout(function(){$("#SRForm").submit();}, DelayInMs);
});
});
<form id="SRForm" method = ... >
<input type="text" name="SRInput" id="SRInput"/>
</form>
(These Timeout
/DelayInMs
vars are still in scope for the events thanks to closures)
Incidentally, this has the added bonus that the timer isn't started until after the first keypress - so you can take as long as you like to start talking).