How AutoSuggest for Tags in StackOverflow Avoids Requests for Each Key Turn

I am trying to implement a similar function, such as the autosuggest function for tags available under the text area on this site, using jQuery. I am trying to understand how requests are sent after a few keystrokes, not after every keystroke. I am using the "keyup" event to trigger a request in my application. I realized that this could lead to too many server hits and could affect performance.

It would be great if someone could explain how I could implement such a thing as stackOverflow, because I did not execute the request on each keyboard.

+3
source share
3 answers
var activity = new ActivityTimer(1000, function() {
    doSearch();
});

$("#searchBox").keyup(function(){ activity.reset() });

function ActivityTimer(deadline, callback) {
    var timer = -1;

    this.reset = function() {
        this.cancel();
        timer = setTimeout(callback, deadline);
    };

    this.cancel = function() {
        if(timer >= 0) {
            clearTimeout(timer);
            timer = -1;
        }
    };
}
+2
source

I have a similar feature in one of my windows applications. When the user enters a character, the timer starts at intervals of 1 second. In the Tick event, the search begins. If the user dials the number again, the timer restarts. Thus, the search is performed only if the keyboard is idle for more than a second.

A short sample (it's in C #, but simple enough to follow):

public partial class Form1 : Form
{
    private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

    public Form1()
    {
        InitializeComponent();

        timer.Interval = 1000;
        timer.Tick += new EventHandler(timer_Tick);
    }

    void timer_Tick(object sender, EventArgs e)
    {
        timer.Stop();

        // Do search, or what ever you want to do
        Console.WriteLine("Searching...");
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (timer.Enabled)
        {
            timer.Stop();
        }

        timer.Start();
    }
}

I am not familiar with Javascript, but the answer from here will help you, I think:

<input name="domain" type="text" id="domain" onKeyUp="javascript:chk_me();">

var timer;
    function chk_me(){

       clearTimeout(timer);
       timer=setTimeout(function validate(){...},1000);
    }
+6
source

, , "Debouncing"

"Debounce" .

var debounce=function(func, threshold, execAsap) {
    var timeout;
    return function debounced () {
        var obj = this, args = arguments;
        function delayed () {
            if (!execAsap)
                func.apply(obj, args);
            timeout = null; 
        };
        if (timeout)
            clearTimeout(timeout);
        else if (execAsap)
            func.apply(obj, args);
        timeout = setTimeout(delayed, threshold || 100); 
    }; 
};

, -, ,

,

$("#search_box").keyup(debounce(function() {
    //do stuff in this function  
}
,350 /*determines the delay in ms*/
,false /*should it execute on first keyup event, 
       or delay the first event until 
       the value in ms specified above*/
));

see Facebook Style AJAX Search for similar use ...

+2
source

Source: https://habr.com/ru/post/1722129/


All Articles