Search code examples
jqueryhtmlajaxusability

Keyup affect the typing in textbox in browser when i send the request. are this possible to stop affect?


in a webpage i have a textbox where user type something and get the result as change the content through ajax request response.

because if i type faster then response maybe late then typing so it' make typing speed slow.

are this possible to stop affect these kind of problem.


Solution

  • to guard against this i usually do something like:

    var to = null;
    $('input').keyup(function()
    {
        if(to!=null)
        {
            clearTimeout(to);
            to = null;
        }
        to = setTimeout(function()
                        {
                           // do my stuff here
                           to = null;
                        },200); 
    });
    

    it waits 200 milliseconds after the last keyup before making the request, if a new keyup occurs the timeout is reset.