I have a method in my controller which I call with ajax.
The method can be called VERY often (depends on the speed of typing of the user).
The user types letters, words. I want the method to be called once the user stops typing.
The parameter of the method is the written text by the user.
In my way, the method gets called a lot of times in a small amount of time.
Is there a way to call the method always (after each letter) but then the method to stop executing immediately if it is called right away?
Example: a user will write slowly 10 letters – the method will be called 10 times.
Example: a user will write fast 10 letters – the method will be called the 10th time OR the method will get called 10 times but will stop executing the first 9 times and finish executing only the 10th time
You can accomplish that using javascript timers. Here’s an example that triggers any logic once it has been half a second since the user’s last keypress.
If the user keeps typing then the timers are constantly being cancelled:
This snippet uses jquery, but you can do it without it. Just use
getElementById()instead of the selectors.