Search code examples
jqueryevent-handlingdelay

jQuery: delay event call in widget


I can't find out how to delay an event handler for a widget. I bind the event like this:

enable: function () {
    this.options.isEnabled = true;
    this.element.bind("keyup", $.proxy(this, "_populateList"));
},

I want to call "_populateList" with a delay. but my attempts with setTimeout are not working.

The "_populateList":

_populateList: function (event) {
    var that = this;
    // do my stuffs
}

Thanks


Solution

  • Try this:

    enable: function () {
        this.options.isEnabled = true;
        var that = this;
        this.element.bind("keyup", function(event){
           $(this)
              .delay(1000) // delayed time in milliseconds
              .queue(function(next){
                 that._populateList(event);
                 next();
              });
        });
    },