html代码如下<script>
$(document).ready(function(){
    $.ajax({
        url:test1.php,
        type:"POST",
        error: function(msg){    
            alert('Error loading XML document'+msg);    
        },
        success: function(data)
        {
           for(i=1;i<data;i++){
             $.ajax({
                    url:test2.php,
                    type:"POST",
                    data:i,
                    error: function(msg){    
                        alert('Error loading XML document'+msg);    
                   },
                    success: function(data)
                   {
                     $('#MESSAGE').html(data)
                    }
             });
           }
        }
    });
})
</script>
<div id=MESSAGE></div>test1.php代码<?php
echo 5;
?>test2.php代码<?php
$num = $_POST['num'];
sleep(3);//等待3秒
echo $num;
?>为什么等待9秒左右在<div id=MESSAGE></div>位置显示最后的值4呢,怎么才能从0开始显示然后一直到1,2,3,4呢?
请大牛帮帮忙 谢谢了。

解决方案 »

  1.   

    PHP我不懂,不过$('#MESSAGE').html(data)是设置值,而不是追加值,应该是之前的值被覆盖了
      

  2.   

    $('#MESSAGE').html($('#MESSAGE').html()+data)
    获取--追加---设置
      

  3.   

    有点明白你的意思了=。=!var count;
            var i = 0;
            $(function() {
                $.ajax(
                    url:text1.php,
                    type: "POST",
                    error: function(msg) {
                        alert('Error loading XML document' + msg);
                    },
                    success: function(data) {
                        count = data;
                        Show();
                    }
                });
            })        function Show() {
                if (i < count) {
                    $.ajax({
                        url: text2.php,
                        type: "POST",
                        data: "name=" + i,
                        error: function(msg) {
                            alert('Error loading XML document' + msg);
                        },
                        success: function(data) {
                            $('#MESSAGE').html(data)
                        }
                    });
                    i++;
                    setTimeout(Show, 3000);
                }else{
                    i=count=null;
                }
            }
    我也是初学=。=!
    我就想的到这个了=。=!
    反正for循环是不好用的=。=!
    所以我把第二个ajax分离出来用延迟调用了=。=!
    你试试吧,不成别怪我哦!
      

  4.   

    请搜索Concurrent.Thread
    如果你系统中大量用到javascript,可以考虑多线程的这个日本东东。Concurrent.Thread.js
      

  5.   


    <script>
    $(document).ready(function(){
        $.ajax({
            url:test1.php,
            type:"POST",
            error: function(msg){    
                alert('Error loading XML document'+msg);    
            },
            success: function(data)
            {
               for(i=1;i<data;i++){
                 $.ajax({
                        url:test2.php,
                        type:"POST",
                        data:i,
                        error: function(msg){    
                            alert('Error loading XML document'+msg);    
                       },
                        success: function(data)
                       {
                         $('#MESSAGE').append(data)
                        }
                 });
               }
            }
        });
    })
    </script>
    <div id=MESSAGE></div>
      

  6.   

    PHP是没有用过,不过,是不是:url:test2.php,
    这个要改为:
    url:'test2.php',