Best alternative to "keyup" event

I have an input textbox that is automatically triggered via AJAX using this Autocompleter Plugin for Mootools.

I'm currently using the keyup event "to get the results via AJAX. It works, but I feel like it's a little awkward as each keypress results in an AJAX request.

I would rather make only ONE AJAX request after the user has finished typing, like after the user has stopped for a bit.

I tried the change event but it was worse, I had to click away from the input text box just to make an AJAX request and then click the input text box again just to trigger "onComplete".

Here's my code:

input.addEvent('keyup', function(e) {
    new Autocompleter.Request.JSON(input.getProperty('id'), url, {
        'postVar': 'search',
        'maxChoices': 6,
        'overflow': false,
        'selectMode': 'pick',
        'delay': 100,
        'forceSelect': false,
        onRequest: function() {
            // do nothing
        },
        onComplete: function() {
            // do nothing
        }
    });
});

      

Hope you guys understand my problem here.

Thank!

+3


source to share


2 answers


Change the option 'delay': 100,

to a larger number.



+1


source


Make the function on keypress then freeze the function for a few seconds as you wanted

code:

input.addEvent('keyup', function(e) {
    setTimeout(function(){
        new Autocompleter.Request.JSON(input.getProperty('id'), url, {
            'postVar': 'search',
            maxChoices: 6,
            overflow :false,
            'selectMode': 'pick',
            delay : 100,
            forceSelect:false,
            onRequest: function() {
                // do nothing
            },
            onComplete: function() {
                // do nothing
            }
        });
    },2000); //1000 = 1sec
});

      



Hope it solves your problem, as you said in your question, "I would rather make only one AJAX request after the user has finished typing, like after the user has stopped for a bit."

For a simpler comment.

Thank...

0


source







All Articles