PHP,jQuery和放大器; Ajax调用乱序 [英] PHP, JQuery & Ajax calls out of order

查看:238
本文介绍了PHP,jQuery和放大器; Ajax调用乱序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery我Ajax调用..我有那附加到一个div ajax调用X量。由PHP的foreach产生这些AJAX负载请求loop..The问题是,他们呈现出它们在阵列中设置的顺序。

I am using Jquery for my ajax calls.. I have x amount of ajax calls that append to a div. These ajax load requests are generated by a php foreach loop..The problem is they render out of the order they are set in the array..

<script type="text/javascript">
function loadPage(target,url,append)
{
    if (append == true) { $.get(url, function(data) { $(target).append(data) }); }
    else { $(target).load(url); }

   return false;
}
</script>  

////// ----- PHP

////// ----- PHP

<?php 

$this->data['sidebar']  = array('login','active_leagues','latest_forum_threads','latest_matches','sponsors');

if (isset($sidebar[0]) && !empty($sidebar[0]))
{
   echo '<div class="right_col">';
   foreach($sidebar as $val)
   {
      echo "<script>loadPage('.right_col','http://dev.banelingnest.com/sidebar/". $val ."',true)</script>";
   }
   echo '</div>';
}

我不知道这样做的原因是Web服务器响应慢比其他一些请求......除此之外,我不知道为什么这可能发生。你有什么想法我怎么能保持请求,以?

I am wonder if the cause of this is the web server responding slower to some requests than others.. Other than that i have no clue why this could be happening. Do you have any thoughts how i could keep the requests in order?

推荐答案

您已经请求前创建的参考点,并把结果附加到他们:

You have to create reference points before the requests, and append the results to them:

var counter = 0;

function loadPage(target,url,append)
{
    if (append == true) {
        var id = "container_"+counter;
        $(target).append("<div id='"+id+"'></div>")
        $.get(url, function(data) { 
            $("#"+id).append(data);
        });
        counter++;
    } else {
        $(target).load(url);
    }

    return false;
}

您的参考元素将被附加到每 loadPage()通话的对象,所以他们会以正确的顺序,并要求能进来任何顺序将在他们的正确的地方进行加载。

Your reference elements will be appended to the target on every loadPage() call, so they will be in the correct order, and the request can come in any order they will be loaded in their right place.

这篇关于PHP,jQuery和放大器; Ajax调用乱序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆