用户不活动几秒钟后执行 ajax 代码

发布于 2024-07-30 01:30:07 字数 986 浏览 6 评论 0原文

我是新来的,所以请对我宽容一些。 这有点令人困惑。 :)

我正在处理搜索输入,其中用户在搜索文本框中输入另一个用户的名称(或名称的一部分),然后返回与该搜索字符串匹配的用户列表。 问题是,当涉及到数万用户时,速度有点慢。 由于这种缓慢,当用户在搜索输入中快速输入名称时,屏幕将开始闪烁每次击键的搜索结果(在用户已经输入搜索字符串之后)。 这就像严重延迟的反应。

例如,如果我输入名称“Wendy”,则搜索字符串“W”(我输入的第一个字符)的搜索结果甚至不会显示。 然后将显示字母“W”的搜索结果,然后是“We”等等,即使我已经输入了全名并且只想查看“Wendy”的结果。

我想做的只是当用户在一段时间内(我想是两秒钟)没有输入任何内容时才执行搜索。 否则,将显示“正在搜索”一词。 Javascript方法的代码如下。 请注意,该代码当前适用于搜索用户,我只需要实现延迟执行。

function updateSearchResults() {
    if($F('searchString').length > 0){
        Element.update($('searchResultsScrollBox'), '<h3>Searching...</h3>'); 
        $('searching').style.display = 'block';
        var url = '<c:url value='ajaxFrontGetUsers.html'/>';
        var ajax = new Ajax.Updater({success: 'searchResults'}, url,
        {method: 'post', parameters: $('searchForm').serialize(true)});
         $('searching').style.display = 'none';
     }
}

我希望这一切都有道理。 预先感谢任何可以提供帮助的人。

I'm new here so please go easy on me. This is somewhat of a confusing situation. :)

I'm working on a search input in which a user enters a name of another user (or part of a name) in a search text box and then a list of users matching that search string is returned. The problem is that it is a bit slow when there are tens of thousands of users involved. Due to this slowness, when the user quickly enters a name into the search input, the screen will begin to flash search results for each key stroke (well after the user has already entered the search string in). It's like a severely delayed reaction.

For instance, if I enter in the name 'Wendy', the search results for the search string 'W' (the first character I entered) will not even be displayed yet. The search results for the letter 'W' will then be displayed, followed by 'We' and so on and so forth even though i've already typed the full name and just want to see the results for 'Wendy'.

What I want to do is only perform the search when the user has not entered anything for a certain period of time (i'm thinking two seconds). Otherwise, the word 'Searching' will be displayed. The code of the Javascript method is below. Just as a note, that code currently works for searching through the users, I just need to implement the delayed execution.

function updateSearchResults() {
    if($F('searchString').length > 0){
        Element.update($('searchResultsScrollBox'), '<h3>Searching...</h3>'); 
        $('searching').style.display = 'block';
        var url = '<c:url value='ajaxFrontGetUsers.html'/>';
        var ajax = new Ajax.Updater({success: 'searchResults'}, url,
        {method: 'post', parameters: $('searchForm').serialize(true)});
         $('searching').style.display = 'none';
     }
}

I hope this all makes sense. Thanks in advance for anyone that can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不回头走下去 2024-08-06 01:30:07

请尝试以下步骤:

  1. 每隔几毫秒检查一下文本框中是否有新数据。
  2. 如果文本框有新文本,请执行 Ajax,并将文本复制到变量(用于在步骤 1 中进行比较)。

如果您想从那里提高性能,请在用户键入内容时激活计时器,并在进行 Ajax 调用时停用计时器。

Try the following steps:

  1. Every few milliseconds, check to see if the textbox has new data in it.
  2. If the textbox has new text, execute your Ajax, and copy the text to a variable (for comparison in step 1).

If you want to improve performance from there, activate the timer whenever the user types something, and deactivate it when the Ajax call is made.

作业与我同在 2024-08-06 01:30:07

嘿,谢谢你的回答。
我最终设置了 500 毫秒的时间间隔,其中 JavaScript 函数将连续检查是否在 500 毫秒的时间段内在搜索字符串中输入了新字符。 如果是,将调用搜索函数来搜索用户输入的字符串。 否则,它会再等待 500 毫秒,直到用户停止输入。 最后,它与您的提议非常相似。
再次感谢!

Hey, thanks for your answer.
I ended up setting 500 millisecond intervals in which a javascript function would continuously check to see if new characters were entered in the search string within that 500 millisecond time period. If they were, the search function would be called to search on the string the user had entered. Otherwise, it would wait another 500 milliseconds until the user had stopped typing. In the end, it's very similar to what you proposed.
Thanks again!

七度光 2024-08-06 01:30:07

或者,您可以在项目上放置一个“onkeypress”事件处理程序,以清除某些全局变量或取消计时器以防止 AJAX 事件触发。 我知道 Prototype 通过它的就地编辑器和“频率”选项为您实现了这一点。 (我相信它设置了一个超时计时器,每次按键后都会取消该计时器。)

Or you could put an "onkeypress"event handler on the item that clears some global variable or cancels a timer to keep the AJAX event from firing. I know Prototype implements this for you via it's in-place editor and the "frequency" option. (I believe it sets a timeout timer that it cancels after every key press.)

掀纱窥君容 2024-08-06 01:30:07

我知道这是一个老问题,但对于其他人来说,我认为你的处理方式是错误的。 我认为你应该为每个 ajax 调用添加日期/时间戳,并跟踪 javascript 中的数据时间戳。 然后,当返回 ajax 调用时,您可以检查日期/时间戳并确保它是最近一次 ajax 调用的结果集。 通过这种方式,您可以立即对每次击键进行 ajax 调用,但仅当 ajax 结果赶上键入时才显示结果。

另外,您是否发送了所有匹配结果? 仅仅字母 W 就相当于数千个? 为了加快 javascript 的速度,您可能应该对数据库中的结果进行排名,并仅返回前 10-20 名。 无论如何,他们不想看到超过 10-20 个结果。

此外,您的 SQL 查询是否最优? 看来查询花费的时间太长了。 如果您进行单面搜索(即 like input+'%')而不是双面搜索(即 like '%' + input + '%' )然后您可以放置​​一些非常好的索引来帮助您。

I know this is an old question, but for others taking a look, I think your going about this the wrong way. I think you should date/time stamp each ajax call, and keep track of the data time stamps in your javascript. Then, when an ajax call is returned you can check the date/time stamp and make sure it is the result set for the most resent ajax call. This way you do make the ajax call for each keystroke immediately, but only display results if the ajax results catches up to the typing.

Also, are you sending over ALL matching results? Like thousands for just the letter W? To speed up the javascript side maybe you should rank the results on the database and return only the top 10-20. The doesn't want to see more than 10-20 results anyways.

Additionally, is your SQL query optimal? Seems like the query is taking too long. If your doing a one sided like search (ie. like input+'%') not a two sided like search (ie. like '%' + input + '%') then there are some really good indexes you can put on the table to help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文