使用ajax延迟输出数据?

发布于 2024-12-24 19:44:48 字数 851 浏览 4 评论 0原文

我正在尝试切换

div 元素当前有一些信息,当用户单击链接时,它将向上滑动,然后向下滑动以显示新信息。

我正在研究 ajax,因此当用户单击链接时,出现的新信息来自另一个页面。

('a').toggle(function(){
    $('.content').slideUp("fast");  
    $.ajax({
        type: "GET",
        url: "URL",
        data: data,
        success: function(data){
                $('.element').delay("slow").slideDown("fast").html(data);
        }
    });
},function(){
    $('.element').slideUp("slow");
    $('.content').delay("slow").slideDown("slow");

});


<div class='content'>
old information
</div>
<div class='element'>
New information from another page, being shown through ajax
</div>

这就是我的脚本的基础知识。现在,当我单击链接时,新信息会在旧信息向上滑动之前立即显示。

关于我应该如何解决这个问题有什么想法或想法吗?也许有更好的写法?

另外,有没有办法删除 .html(data)?那么当我滑回原始文件夹时,它会消失吗?或者我只需要使用 .hide() 函数?也许.remove()?

谢谢!

I'm trying to work on a toggle

a div element currently has some information and when a user clicks a link, it will slide up, and slide down with new information.

I'm working on ajax so when user clicks on the link, the new information that appears is coming from another page.

('a').toggle(function(){
    $('.content').slideUp("fast");  
    $.ajax({
        type: "GET",
        url: "URL",
        data: data,
        success: function(data){
                $('.element').delay("slow").slideDown("fast").html(data);
        }
    });
},function(){
    $('.element').slideUp("slow");
    $('.content').delay("slow").slideDown("slow");

});


<div class='content'>
old information
</div>
<div class='element'>
New information from another page, being shown through ajax
</div>

Thats the basics of my script. Right now when I click on the link, the new information shows right away before the old information gets to slide up.

Any thoughts or ideas on how I should go about this? Maybe a better way to write this?

Also, is there a way to remove the .html(data)? so when I slide back to the original folder, it'll disappear? Or would I just have to use the .hide() function? Maybe .remove()?

Thanks!

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

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

发布评论

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

评论(1

听闻余生 2024-12-31 19:44:48

听起来您可能想在幻灯片上使用回调。一旦滑动函数完成,它将运行回调函数。
所以是这样的:

$('a').toggle(function(){
    $('.content').slideUp("fast", function(){
        $.ajax({
            type: "GET",
            url: "URL",
            data: data,
            success: function(data){
                $('.element').delay("slow").slideDown("fast").html(data);
            }
        });
    });

虽然这样做的缺点是在上滑发生之前它不会通过 AJAX 加载结果,如果让你等待额外的时间,这是有可能的,但是,这听起来不像是等待问题,所以这可能就是您所需要的。

如果我遇到了同样的问题,听起来就像您遇到的那样,我可能会创建 2 个变量,一个用于保存幻灯片状态,另一个用于保存从 AJAX 返回的 HTML。
然后,我将使 ajax 成功回调和 Slideup 回调运行一个函数,如果满足 2 个条件,该函数将插入 HTML 并向下滑动:1) 上滑已完成,2) AJAX 请求已返回结果。

这样,请求和上滑/滑下将尽可能快地协同工作,但不会重叠。

// here's an example of the function you would have both slideUp and ajax success callbacks run
var $html,
    $slide = false; // false indicates we're not in the process of a slideup

function process_data()
{
    // only process the slidedown if we're not in the process of a slideup and we have the html we're after
    if (!$slide && $html)
    {
        $('.element').slideDown('fast').html($html);
        // make sure to empty $html afterwards so we don't assume we have data next time this runs
        $html = '';
    }
}

所以你的幻灯片将在回调中包含上述函数

$slide = true;
$('.content').slideUp('fast', function(){
     // the slideup is completed, so set the slide back to false
     $slide = false;

    // process data
     process_data();
});

,并且你的ajax成功也将使用process_data

success: function(data){
    // store the returning data in the html variable
    $html = data;

    // process data
    process_data();

It sounds like you might want to use the callback on your slideup. It will run the callback function once the slideup function is complete.
So something like this:

$('a').toggle(function(){
    $('.content').slideUp("fast", function(){
        $.ajax({
            type: "GET",
            url: "URL",
            data: data,
            success: function(data){
                $('.element').delay("slow").slideDown("fast").html(data);
            }
        });
    });

Although the drawback of that is it wouldn't load results through AJAX until after the slideup happens, which has the possibility if making you wait an additional length of time, however, it doesn't sound like waiting the problem, so that might be all you need.

If I were running into the same issue it sounds like you're running into, I would probably create 2 variables, one to hold the slideup state and one to hold the HTML returning from AJAX.
I would then make both the ajax success callback and the slideup callback run a function that would insert the HTML and slide down if 2 conditions are met: 1) the slideup is completed and 2) the AJAX request has returned a result.

This way, both the request and the slideup/slidedown will work together as fast as possible but without overlapping.

// here's an example of the function you would have both slideUp and ajax success callbacks run
var $html,
    $slide = false; // false indicates we're not in the process of a slideup

function process_data()
{
    // only process the slidedown if we're not in the process of a slideup and we have the html we're after
    if (!$slide && $html)
    {
        $('.element').slideDown('fast').html($html);
        // make sure to empty $html afterwards so we don't assume we have data next time this runs
        $html = '';
    }
}

so your slideup would include the above function in the callback

$slide = true;
$('.content').slideUp('fast', function(){
     // the slideup is completed, so set the slide back to false
     $slide = false;

    // process data
     process_data();
});

and your ajax success would also use process_data

success: function(data){
    // store the returning data in the html variable
    $html = data;

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