English 中文(简体)
Jquery 太多
原标题:Jquery too many ajax request fired

我有以下用 j子建造的法典。 如果用户拷贝和过去是你的管,那么如果是用来提取录像机,则在舱里采用SapVideoId(str)方法。 采用id,即获得视频图像图片标题和内容。

当文本箱和(......)“#url”的长度超过10年,即提出“jax”要求。 因此,Ajax的请求正在发挥作用。 但现在还有另一个问题。 文本箱有10多个特性,第一次提出2个jax要求(用火力测试)。 当用户进入更多的特征时,提出了许多亚希克斯要求。

因此,这将减缓最后一项贾汉斯请求的进程。 我只想获得你的链接数据,并显示用户能够增加标题和内容的建议。 旧的脸谱视频连接也一样。 没有人在改进守则方面有更好的建议?

jQuery(document).ready(
    function(){
        $("#url").keyup(function() {
            var $t1 =  $("#url").val();
            var $length = $t1.length;
            var $data;

            $("#title").val($length);
            $("#content").val($t1);

            if($length==0){
                alert( zero value );
                return;
            }

            if($length>10){
                $data = $.ajax({
                url:  <?php echo $this->Html->url(array("action" => "retrieveVideoFeed"));?> ,
                dataType: "json",
                data: {
                    vid: getVideoId($t1)
                },
                success: function(data) {
                                            alert( success in getting data );
                }
            });

                return;
            }

        });

            function getVideoId(str) {
                var urlRegex = /(http|https)://(w+:{0,1}w*@)?(S+)(:[0-9]+)?(/|/([w#!:.?+=&%@!-/]))?/;
                if (urlRegex.test(str) && str.indexOf( v= ) != -1)
                {
                    return str.split( v= )[1].substr(0, 11); // get 11-char youtube video id
                } else if (str.length == 11) {
                    return str;
                }
                return null;
            }
    }
);
最佳回答

You could cache the calls and use blur event and not keyup: you are firing a lot of AJAX call because keyup() fires an event each time a key is pressed, you should use blur that fires an event when an input loses focus. If you cache the calls in an object you can avoid a lot of repeated calls

   var cacheUrl = {};

   $("#url").blur(function() {
        var $t1 =  $("#url").val();
        var $length = $t1.length;
        var $data;

        $("#title").val($length);
        $("#content").val($t1);

        if($length==0){
            alert( zero value );
            return;
        }

        if(cacheUrls[$t1] !== undefined && $length>10){
            $data = $.ajax({
            url:  <?php echo $this->Html->url(array("action" => "retrieveVideoFeed"));?> ,
            dataType: "json",
            data: {
                vid: getVideoId($t1)
            },
            success: function(data) {
                    //save the data to avoid a future call
                    cacheUrls[$t1] = data;
                                        alert( success in getting data );
            }
        });

            return;
        }elseif ($length>10){
            //you already have the data in  cacheUrls[$t1] 
        }

    });

EDIT 如果你想要利用提交钥匙开始搜寻,那么当你报到时,就会引发模糊事件:

$("#url").keypress(function(e){
    if(e.which == 13){
       $(this).blur();
       return false;
    }
});
问题回答

I think many ajax requests are fired because you are using $("#url").keyup(function() so that for every key event in url input the particular funciton will exectue.So, as per i know better to use focusout method instead of keyup.

如果你坚持<代码>keyup-Event,你可能希望使用ajaxmanager-plugin,用于管理查询或限制同时申请的数量。

    $.manageAjax.create( myAjaxManager , {
        queue: true,
        cacheResponse: false,
        maxRequests: 1,
        queue:  clear 
    });
    ....
    if($length>10){
        $data = $.manageAjax.add({ ...

这将防止在使用者正在打字时使用分管。 一旦他停止申请,不会流产,结果将显示。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签