Search code examples
javascriptjqueryjquery-uijquery-ui-autocomplete

How can I close an autocomplete menu after 10 seconds regardless of where the cursor or focus is?


I have a JQuery UI autocomplete function and want the menu to stay open for 10 seconds, regardless of where the focus is. I have:

$("#term").autocomplete({
  source: function(request, response) {
    $.ajax({
      url: "https://example.com/",
      dataType: "json",
      data: {
        q: request.term
      },
      success: function(data) {
        response(data);
      }
    });
  },
  close: function() {
    $('.ui-autocomplete').show().delay(10000).hide();
  }  
});

This, unfortunately, makes it so it closes immediately once the ac area loses focus. If I remove the .delay(10000).hide() part it stays open indefinitely but I need to close it after a few seconds. Thanks!


Solution

  • setTimeout(function(){
         $("#term").autocomplete( "disable" );
    }, 10000);