javascript - Auto submit form after x mili seconds of inactive keyboard -


In my form I have a set of input boxes where the user can input a value. If any of these boxes are changed, the form is automatically submitted.

The problem is that the user lives in the previous field, takes the mouse and presses the OK button (second form) without leaving the text box first. The event of change is not triggered and the old, wrong values ​​meet on the next page.

I want to trigger an on-event event after some milliseconds of the idle keyboard. Just like most auto-complete plugins
I think I can apply a timer, when you enter an input field and each keystroke is controlled at all times and then when it becomes zero, then the event of that time Is triggered. >

I am not ready to invent the wheel again and wondered if such a function is available somewhere. Suggestion?

I had a similar problem and currently has a jQuery plugin built into an internal application

If you are not using jQuery, the code is still favorable to something else.

  handle jQuery.fnBordChanged = function (nDelay) {// Utility function to ignore whether the keyboard event should be ignored, ignore the function (event) {var MapIgnoredKeys = {9: true, // Tab 16: true, 17: true, 18: true, / shift, alt, ctrl 37: true, 38: true, 39: true, 40: true, // arrow 91: True, 92: True, 93: True // Windows Key}; Unrecognized map of return [incident that]; } // utility work to fire our change event if the value actually changed the function, then the fire changer ($ element) {if ($ element.val ()! = JQuery.data ($ element [0] , "ValueLast")) {jQuery .data ($ element [0], "value", $ element.val ()) $ element.trigger ("change"); }} // Currently the run timeout, // will be accessed with Closure time = 0; // Pre-set timeout function to cancel the utility function explicit foregoing timeout () {if (timeout) {clearTimeout (timeout); }} For this return Keyboard (function) {If (unknown (incident)) Return; // user pressed a key; Now stop timeout for clear; Pre-timeTime (return; return;)} .keyup (event) {if (Ignore Event (Event)); // Start the time-table to fire our event after some time of inactivity / End before the end of the run time Expiry Timeout (); var $ self = $ (This); timeout = setTimeout (function) {FireChange ($ self)}, nDelay);}) .c Hange (function () {// fire change // // Instead of firing the incident, use our function / / because we want to check that the value has actually changed / our previous event./ When a change in the browser changes the event / fire we have already removed the event due to the timeout fire change ($ (this));}  

Usage Handleboard Variation (300) .change (function () {// value has changed!});


Comments