Ajax call within a 'for' loop


Ajax call within a 'for' loop

是否可以在循环中使用ajax调用?
这里我想,当我点击按钮,然后ajax调用将调用10次。下面是我的代码。我不知道这是不是一个好习惯。

但是我需要这种解决方案。我该怎么做呢?谢谢你。

HTML:

  <button>Click</button>
AJAX:

  $(document).ready(function(e) {
            $('button').click(function(){
                  for(var i=1;i<=10;i++){        //is it possible ?
                    $.ajax({
                        type:'POST',
                        url : "a.php",
                        data:'a='+$('.div1').text(),
                        success: function(result){
                                $('.div1').html(result);
                            }                               
                    });
                  }
                });
        });

a.php:

        <?php
             if(!isset($_POST['a']) || $_POST['a'] == ""){
                $a = 0;
             }
             else{
                $a = $_POST['a'] + 1;   
             }
             echo $a;
             exit();
         ?>

try this:

$(document).ready(function(e) {
        var ajaxCalled=0;
        $('button').click(function(){
                $.ajax({
                    type:'POST',
                    url : "a.php",
                    data:'a='+$('.div1').text(),
                    success: function(result){
                            $('.div1').html(result);
                            ajaxCalled++;
                            if(ajaxCalled<=10){
                                $('button').trigger('click');
                            }
                            else{
                                ajaxCalled=0;
                                return false;
                            }
                        }               
                });
            });
    });