最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Avoid multiple autocomplete calls by wrapping it with SetTimeOut - Stack Overflow

programmeradmin2浏览0评论

Here's my issue : using an autoplete jQuery plugin, I'd like to avoid multiple ajax requests when user strikes his keynoard by surrounding the

$('#query1').autoplete({
                    serviceUrl:'/actions/autoplete?population=salon',
                    minChars:3, 
                    maxHeight:300,
                    width:200,
                    clearCache:true,
                    onSelect: function(suggestions,data){ $(".btn1").attr("href", "${pageContext.request.contextPath}/actions/espaceClients?participantId=" + data) }
                });

with something like

                var search = false;
            $('#query1, #query2, #query3').keyup(function(){
                if (!search){
                    search = true;
                }
                if (search) {
                    search = false;
                    autopleteThem();
                }
            });

A you can see, above code is stupid, but it kinda shows what i'm trying to do.

In simple words, if user dosen't type anything else in a certain period of time, then you can call autoplete.

I hope i'm being clear, as my brains are a mess...

Here's my issue : using an autoplete jQuery plugin, I'd like to avoid multiple ajax requests when user strikes his keynoard by surrounding the

$('#query1').autoplete({
                    serviceUrl:'/actions/autoplete?population=salon',
                    minChars:3, 
                    maxHeight:300,
                    width:200,
                    clearCache:true,
                    onSelect: function(suggestions,data){ $(".btn1").attr("href", "${pageContext.request.contextPath}/actions/espaceClients?participantId=" + data) }
                });

with something like

                var search = false;
            $('#query1, #query2, #query3').keyup(function(){
                if (!search){
                    search = true;
                }
                if (search) {
                    search = false;
                    autopleteThem();
                }
            });

A you can see, above code is stupid, but it kinda shows what i'm trying to do.

In simple words, if user dosen't type anything else in a certain period of time, then you can call autoplete.

I hope i'm being clear, as my brains are a mess...

Share Improve this question asked May 3, 2010 at 12:09 pixelboypixelboy 7291 gold badge13 silver badges37 bronze badges 1
  • i am after similar what you doing, would you please share the sample code? are you using asp? – Nick Kahn Commented May 4, 2010 at 14:11
Add a ment  | 

2 Answers 2

Reset to default 4

Use the deferRequestBy option on that plugin, like this:

$('#query1').autoplete({
                serviceUrl:'/actions/autoplete?population=salon',
                minChars:3, 
                maxHeight:300,
                width:200,
                clearCache:true,
                deferRequestBy: 200, //200ms
                onSelect: function(suggestions,data){ $(".btn1").attr("href", "${pageContext.request.contextPath}/actions/espaceClients?participantId=" + data) }
            });

Do you really want to prevent multiple simultanious requests? This will inhibit the usefulness of this plugin. You can either add a delay to the request, or use a plugin such as AjaxQueue to ensure the results e back to the browser in the right order.

To use the built-in delay parameter (from the manual):

The delay in milliseconds the Autoplete waits after a keystroke to activate itself. A zero-delay makes sense for local data (more responsive), but can produce a lot of load for remote data, while being less responsive.

Initialize a autoplete with the delay option specified.

$( ".selector" ).autoplete({ delay: 0 });

Get or set the delay option, after init.

//getter
var delay = $( ".selector" ).autoplete( "option", "delay" );
//setter
$( ".selector" ).autoplete( "option", "delay", 0 );

The equivalent of the delay parameter for the autoplete plugin you're using is

deferRequestBy
发布评论

评论列表(0)

  1. 暂无评论