Submit form after X seconds without entering in a specific field

I use Speech-to-Text software to enter a field, since there is no screen or mouse or keyboard, I need to send this form after 3 seconds without entering (actually speaking) in this field, but the field should not be empty

I use PHP, but I think the solution is JavaScript or jQuery, I don’t know much in these two, so I would appreciate it if you could explain how to use it.

+5
source share
3 answers
$(function() {
    var time = 0;
    $("textarea").keyup(function() {
        time = 0;
    });

    var int = self.setInterval(function() {
        var s = $("textarea").val();
        if (time++ == 3) {
            if (s != "") alert(s);
            time = 0;
        }
    }, 1000);
});​

Demo

UPDATE:

As @Tim Down said ,

The keypress event is designed for handling the a character typed by the user 
rather than detecting keyboard activity and the delete and backspace keys 
do not generate characters. Some browsers blur this line somewhat but 
the general principle is that the keyup and keydown events are there to detect 
any key being pressed and telling you about which key it is while keypress is 
for detecting an actual character being typed.

So, it would be better if you used keyup instead of pressing a key, then your timer will clear for any key.

+4
source

, . ,

, jQuery, :

$('#ElementId').keypress(function() {
    /* Your code */
});

setTimeout() 3 ( )

, , jQuery submit(), ,

focus(), , SR "" " ,

, JS, ,

, - ():

$(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>

( Timeout/DelayInMs vars - closures) , , , - , ).

+5

I think you could use http://narf.pl/jquery-typing/jquery.typing-0.2.0.min.js

take a look at http://narf.pl/jquery-typing/

$(':text').typing({
    start: function (event, $elem) {
       // do nothing
    },
    stop: function (event, $elem) {
       // send your form
    },
    delay: 3000
});

Hope this helps

0
source

All Articles