I want to save the information entered by the user in the form. To update it when you refresh the page.

I keep in session.

Only if I put an event on onkeyup, then JS sends each entered letter.

Question. And how to make a delay of 2 seconds. What would send a request after the user stops to enter information?

  • onchange event not suitable? - Kazantsev
  • Not. Since the user can write text and not leave the form. And when the page is updated or the Internet disappears, its input will be lost. - EduardRST

3 answers 3

If you need to send data only if the user has stopped entering data, then you can do this:

 $('body').on('keyup', "#контейнер", function(I) { switch (I.keyCode) { default: searchDelay(function() { $.ajax({ //ДЕЛАЕТЕ ВАШ ЗАПРОС }); }, 1000); break; } }); var searchDelay = (function() { var timer = 0; return function(callback, ms) { clearTimeout(timer); timer = setTimeout(callback, ms); }; })(); 

  • Thank you very much! Just what you need - EduardRST

For this, it is better to use onblur = your function (), the function will be launched when you lose focus on the input field.

  • It will not work. Since the user can write text and not leave the form. And when the page is updated or the Internet disappears, its input will be lost. - EduardRST
  • @Anton This is also not an option, as the user can finish input but the focus can still be in input - Raz Galstyan
  • Then you can make a delay of 2-5 seconds, that if you stop typing, then start the function, for this, see SetTimeout (), it is very convenient. - Anton
  • That is, after each character input, enter the function and start the timer for 2-5 seconds, and upon their expiration send the entered string. - Anton
  • @Anton, but do not tell me how to do it correctly. And then my delay works, but after 2 seconds, it still sends all the characters without exception - EduardRST

And why do you even need to store user input on the server? Keep it with the user (in localStorage for example). And on the server, save the regular method when the user wants to send the form to the server.

  • Better on the server. So safer - EduardRST