简体   繁体   English

双异步AJAX函数

[英]Double Asynchronous AJAX Function

So I am trying to write a javascript script to dynamically load pages on my site without refreshing the page. 所以我试着编写一个javascript脚本来动态加载我的网站上的页面而不刷新页面。 Basically I have a function which fades out the content div, loads the new content, switches the content, then fades it in. The content is loaded using an asynchronous AJAX request, which on success calls the function that fades the content in. The fadein function should not run until the load function AND the fadeout function are both done. 基本上我有一个功能,淡出内容div,加载新内​​容,切换内容,然后淡入。内容使用异步AJAX请求加载,成功调用淡化内容的函数.fadein在加载函数和淡出功能都完成之前,函数不应该运行。 I want the page to be loading the whole time the content is fading out, but right now my fading animation is frozen. 我希望页面在内容淡出的整个过程中加载,但是现在我的淡入淡出动画被冻结了。 I've tried a bunch of different ways to figure this out but nothing is working so far... 我已经尝试了一些不同的方法来解决这个问题,但到目前为止还没有任何工作......

Any ideas? 有任何想法吗?

This is just a matter of waiting for two events to complete and, when they're both complete, taking action. 这只是等待两个事件完成的问题,当它们完成时,采取行动。 See my answer to this other question . 看看我对这个问题的回答 Basically: 基本上:

Have a flag for each event, initially false. 每个事件都有一个标志,最初是假的。 Hook the two events in question (the completion callback on the animation, and the ajax success). 挂钩有问题的两个事件(动画的完成回调和ajax成功)。 Each event sets its flag and calls a function. 每个事件都设置其标志并调用一个函数。 That function checks the flags: If both are set, it does something (in your case, sets the new content). 该函数检查标志:如果两者都设置,它会执行某些操作(在您的情况下,设置新内容)。 See the link for a code example. 请参阅链接以获取代码示例。

I have a site which does something similar. 我有一个类似的网站。 The function to slide out, replace content, and slide in is as follows: 滑出,替换内容和滑入的功能如下:

var loadAnAjaxArticle = function (url) {
    var removeParentArticle = function () {
        // I don't like doing this, but so far haven't found a
        // particularly elegant way to replace the article
        // when calling load(), only append it.
        $('article').first().html($('article').last().html());
    };

    var showArticle = function (response, status, xhr) {
        if (status == "error") {
            $('article').first().html('<h2>There was an error processing your request.</h2>');
            $('article').first().slideDown('slow');
        } else {
            removeParentArticle();
            $('article').first().slideDown('slow');
        }
    };

    var loadArticle = function () {
        $('article').first().load(url + ' article', showArticle);
    };

    $('article').first().slideUp('slow', loadArticle);
}

Basically, it calls .slideUp to hide the content, providing a call-back to another function. 基本上,它调用.slideUp来隐藏内容,提供对另一个函数的回调。 That function calls .load to refresh the content, providing a call-back to another function. 该函数调用.load来刷新内容,提供对另一个函数的回调。 That function does a little manipulation of the DOM to make it look right (it's early in development, I haven't made it more elegant yet), and calls .slideDown to show the content again. 函数对DOM进行了一些操作以使其看起来正确(它在开发早期,我还没有使它更优雅),并调用.slideDown再次显示内容。

This is easy to do in this case because each function I'm using accepts a call-back. 在这种情况下这很容易做到,因为我正在使用的每个函数都接受回调。 If the functions you're using have the same capability, you should be able to chain them in this manner. 如果您使用的功能具有相同的功能,您应该能够以这种方式链接它们。

You just need to set a flag and have both completion functions check to see if both events are done. 您只需设置一个标志并检查两个完成功能,以查看是否完成了两个事件。 Whichever completion function finds both are done, triggers the next action. 无论哪个完成功能都完成,都会触发下一个动作。

You haven't give us any specifics of your code, but here's the general idea: 您没有向我们提供您的代码的任何细节,但这是一般的想法:

function loadNewPage(args) {

    var fadeDone = false;
    var ajaxData;

    function processAjaxDataWhenReady() {
        // if both have completed, then process the ajaxData and start the fadeIn
        if (fadeDone && ajaxData) {
            // process the ajaxData here
            $("#myContent").fadeIn();
        }
    }

    // start the fadeOut
    $("#myContent").fadeOut(function() {
        fadeDone = true;
        processAjaxDataWhenReady();
    });

    // start the ajax call
    $.ajax({...}, function(data) {
        ajaxData = data;
        processAjaxDataWhenReady();
    })

}

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

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