jQuery UI 自动完成:如何启动异步进程并在完成之前退出它

发布于 2024-10-08 04:54:50 字数 254 浏览 0 评论 0原文

我正在使用 jQuery UI 自动完成来构建搜索界面。

我定义了一个自定义函数,它返回搜索结果并在按下按键时自动更新它们,但是,有时此自定义函数在按下下一个键之前不会完成(即,当我已经编写“Washi”时,它仍在处理“Wash”) (在编写“华盛顿”的过程中)

我希望每次调用自动完成“源”事件时都取消前一个函数(Wash)并启动新函数(Washi)并使用下一个调用取消函数(Washi)。启动功能(Washin)等等

这样的事情是怎么做的呢?

I am using jQuery UI Autocomplete to build a search interface.

I have defined a custom function which returns search results and updates them automatically as keys are pressed however, sometimes this custom function doesn't complete before the next key is pressed (i.e. its still processing "Wash" when I have already written "Washi" (on the way to writing "Washington").

I would like every call to the autocomplete "source" event to cancel the previous function(Wash) and start the new function(Washi). With the next call cancelling function(Washi) and starting function (Washin) and so on.

How is this kind of thing done?

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

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

发布评论

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

评论(1

长安忆 2024-10-15 04:54:50

您应该能够执行以下操作:

var lastXhr;
$( "#myAutocomplete" ).autocomplete({
    source: function( request, response ) {
        if (lastXhr) lastXhr.abort();
        lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
            if ( xhr === lastXhr ) {
                response( data );
            }
        });
    }
});

lastXhr var 存储最新的 xhr 请求。如果有一组并且源函数被调用,那么它会中止 lastXhr 请求并发出一个新请求。当ajax请求返回时,如果确保它与lastXhr匹配,如果不匹配,则不会调用response()函数。

You should be able to do something like this:

var lastXhr;
$( "#myAutocomplete" ).autocomplete({
    source: function( request, response ) {
        if (lastXhr) lastXhr.abort();
        lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
            if ( xhr === lastXhr ) {
                response( data );
            }
        });
    }
});

the lastXhr var stores the most current xhr request. If there is one set and the source function is called then it aborts the lastXhr request and makes a new one. When the ajax request returns if make sure that it matches the lastXhr, if not then it doesn't call the response() function.

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