无论光标或焦点在哪里,如何在 10 秒后关闭自动完成菜单?

Posted

技术标签:

【中文标题】无论光标或焦点在哪里,如何在 10 秒后关闭自动完成菜单?【英文标题】:How can I close an autocomplete menu after 10 seconds regardless of where the cursor or focus is? 【发布时间】:2020-07-07 15:33:36 【问题描述】:

我有一个 JQuery UI 自动完成功能,希望菜单保持打开状态 10 秒,无论焦点在哪里。我有:

$("#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();
    
);

不幸的是,一旦交流区域失去焦点,它就会立即关闭。如果我删除 .delay(10000).hide() 部分,它会无限期地保持打开状态,但我需要在几秒钟后关闭它。谢谢!

【问题讨论】:

你试过使用setTimeout()吗? 【参考方案1】:
setTimeout(function()
     $("#term").autocomplete( "disable" );
, 10000);

【讨论】:

以上是关于无论光标或焦点在哪里,如何在 10 秒后关闭自动完成菜单?的主要内容,如果未能解决你的问题,请参考以下文章