There is a plugin validation of form fields when you enter the user. When entering into any field, the keyup event sends an ajax request to the server. In callback, if the field is not valid, we show an error, also hide / show the submit button depending on the validity of the current and other fields.
As a result, of course full of ajax requests occur when a user enters. All this does not seem to be particularly slow, but I would like to make it more optimal, that is, that requests would not be sent more often than for example once per second.
How to do this, for example, using setInterval Tk, there is a specific logic that is very specific to our project. I show only the main thing.
jQuery.fn.nickmsk = function(o){ //console.log(o); this.bind('keyup.nickmsk', function(e) { this.is_backspace = false; if (e.keyCode == 8) this.is_backspace = true; var k = e.charCode || e.keyCode || e.which; if (e.ctrlKey || e.altKey || e.metaKey) { //Ignore return true; } else if ((k >= 32 && k <= 125) || k > 186) { //typeable characters //var c = String.fromCharCode(k); } else if(e.keyCode != 8) { return true; } if(e.keyCode >= 37 && e.keyCode <= 40) { return true; } var fldtext = $(this).val(); //Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΏΠΎΠ»Ρ ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠ²ΡΠ·Π°Π½ ΠΏΠ»Π°Π³ΠΈΠ½ fldtext = fldtext.replace('&', '%26'); var form = o.form; //ΡΠΎΡΠΌΠ° ΠΊΠΎΡΠΎΡΠ°Ρ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΠΏΠΎΠ»Π΅. var qs = "&form=" + form + "&checkstr=" + fldtext; $.ajax( { url: '/ajax/nickname.php', data: qs, dataType: 'json', type: 'post', beforeSend: function() { //$("#check_av").show(); }, success: function (json) { //ΠΠΎΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΠΈΠ»ΠΈ ΡΠΊΡΡΠ²Π°Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΡ ΠΈ ΠΊΠ½ΠΎΠΏΠΊΡ ΠΎΡΠΏΡΠ°Π²ΠΈΡΡ. } }); } }); }; The plugin is called so
$("#login").unbind(); $("#login").nickmsk({form: $("#regist_form") }); $("#passwd1").unbind(); $("#passwd1").nickmsk({form: $("#regist_form") }); Actually it is more than parameters. But that is not the point. What would you do here in SetTimeout what would be when entering and checking each field there was a pause between requests for 1 sec