简体   繁体   English

为什么我的JQuery AJAX调用执行两次,但前提是不首先调用它?

[英]Why does my JQuery AJAX call execute twice, but only if it's not called first?

On my portfolio website, I am using a jQuery .ajax() call to pull in my portfolio pieces via XML. 在我的投资组合网站上,我正在使用jQuery .ajax()调用以通过XML提取我的投资组合。

My issue is that after a fresh page load, if the "portfolio" link is clicked first , then the portfolio pieces are pulled in normally. 我的问题是,在重新加载页面后,如果首先单击“投资组合”链接,那么通常会拉入投资组合部分。 If, after a fresh page load, the "portfolio" link is clicked after any of the other links, then the portfolio pieces are pulled in twice. 如果在重新加载页面 ,在其他链接中的任意一个之后单击“投资组合”链接,则投资组合部分将被拉入两次。

You can see the issue for yourself on my site: Transhuman Creative 您可以在我的网站上看到自己的问题: 超人创意

Here is the code that figures out which navigation link is clicked based on its rel attribute: 以下是根据其rel属性确定单击哪个导航链接的代码:

$("#nav a").click( function () {
    if($(this).attr("rel") == "blog") {
        return false;
    }else{
        $("#nav a").removeClass("selected");
        $(this).addClass("selected");
        setBlock($(this).attr("rel"));
    }
});

After a link is clicked, it is processed by theThe setBlock() function, which hides existing content and calls the processBlock() function to load content. 单击链接后,将由setBlock()函数处理该链接,该函数将隐藏现有内容并调用processBlock()函数以加载内容。

function setBlock(block) {
    if(firstNav) {
        processBlock(block);
        firstNav = false;
    }
    else
    {
        if($(".tab").length > 0 && $(".tab").is(":hidden") == false) {
            $(".hidable").fadeOut();
            $(".tab").fadeOut(function(){
                processBlock(block);
            });
        }
        else {
            $(".hidable").fadeOut(function (){
                processBlock(block);
            });
        }       
    }
}

The processBlock() function waits 500ms to let the animation finish, then either shows the block of content or calls the loadItems() function to load the portfolio data. processBlock()函数等待500毫秒以使动画结束,然后显示内容块或调用loadItems()函数以加载投资组合数据。

function processBlock(block) {
    var s = setInterval( function () {
        if (block == "portfolio") {         
            loadItems();            
        }else{          
            $("." + block).fadeIn();
        }
        clearInterval(s);
    }, 500);
}

And finally, the .ajax() call is in the loadItems() function. 最后, .ajax()调用位于loadItems()函数中。 After loading the porfolio data from the XML file, it calls the tabFade() function to parse the data and generate the HTML for the portfolio pieces. 从XML文件加载组合数据后,它将调用tabFade()函数来解析数据并生成投资组合的HTML。 The variable firstCall is initially set to true , and it is meant to prevent the portfolio data from being reloaded if it's already in memory: 变量firstCall最初设置为true ,旨在防止已在内存中的投资组合数据重新加载:

function loadItems() {
    if (firstCall) {
        $.ajax({
            type: "GET",
            url: "data/portfolio.xml?ver=1.11",
            cache: false,
            dataType: "xml",
            success: function(xml){
                $(xml).find('item').each(function(){
                    $("#main").append(addItem($(this)));
                });
                tabFade();
                firstCall = false;
            }
        });
    }else{
        tabFade();
    }
}

Any thoughts on what might be causing the double load issue? 关于什么可能导致双重负荷问题的任何想法? Thanks for your help. 谢谢你的帮助。

I believe it would be better to set the firstCall variable right inside of the if condition. 我相信最好在if条件内设置firstCall变量。 Otherwise it waits 500+ milliseconds before being set and only gets set once the ajax request completes. 否则,它将等待500毫秒以上才被设置,并且仅在ajax请求完成后才被设置。

function loadItems() {
    if (firstCall) {
        firstCall = false; // Put the assignment here before waiting.
        $.ajax({
            type: "GET",
            url: "data/portfolio.xml?ver=1.11",
            cache: false,
            dataType: "xml",
            success: function(xml){
                $(xml).find('item').each(function(){
                    $("#main").append(addItem($(this)));
                });
                tabFade();
                //firstCall = false;
            }
        });
    }else{
        tabFade();
    }
} 

Try using setTimeout instead of setInterval. 尝试使用setTimeout而不是setInterval。 You probably want to use setTimeout anyway as I don't think you want to run the code more than once? 您可能仍然想使用setTimeout,因为我不希望您多次运行代码?

It could be that it's running that code twice and making two ajax calls as it hasn't responded within 500ms. 可能是因为它在500毫秒内没有响应,因此两次运行了该代码并进行了两次ajax调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM