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

  • Client validation itself was done in order not to burden the server once again, and you do the opposite. ))) - cpp_user
  • Client authorization is all the same because you do not need to repeat the server for security purposes. So I simplify the task to check only on the server, and not to do some of the checks on the client, duplicate them on the server, and some that require access to the database on the server. Do not recommend this? Even if you check only the uniqueness of the E-mail addressing the server, all the same, after each input of a character by the user, it causes an ajax request. Here also it would be desirable that requests went not one after another, but with an interval. - htclog81 9:59 pm
  • Still, then you will need to re-double-check everything on the server. What is the gain? On the client you need to check what you can check, uniqueness is no longer a client problem. - cpp_user pm
  • I probably did not write very clearly. I see two approaches. 1) Check the completion of the field, the format of the mail on the client, check the uniqueness of ajax with the request to the server, then when sending the form, check the completeness and format and uniqueness on the server. As far as I understand, they usually do this way. 2) Check and fill out the fields and the format and uniqueness of ajax request to the server and recheck everything again on the server when sending the form. So done by me. This is subjectively simpler and there is no time to redo it as in option 1. But I would like that too often the requests to the server did not go. - htclog81

0