按顺序延迟一段时间后用jquery执行PHP文件


execute php file in jquery after a time delay in sequence

我有一个jquery函数,我需要在以下方式工作

1. call a php file
2. log the values return by php file
3. have time delay
      again repeat the above process for number of time

为此我编写了以下jquery和PHP文件

<script type="text/javascript">
$(document).ready(function() {
listsValues="1,10,20";
n=0;
msgids = listsValues.split(',');
$.each(msgids, function() {
html="TEST MESSAGE";
 n++;
setTimeout(function() {
$.ajax({
                type: "POST",
                url: "test1.php",
                cache:false,
                data:"id="+ msgids[n],
                dataType:'json',
                success: function(json)
                {
                var foo = json.foo;
                uemail = foo.split(',');
                console.log(uemail)
                }
            });
 }, 1000);
 });
  });
</script> 
这里是test1.php
<?php
$id=$_POST['id'];
$val="";
for($i=1;$i<=$id;$i++) { 
$val.=$i;
}
$return["foo"] =$val;
print stripslashes(json_encode($return));
?>

,但这不是我想要的方式工作,当我执行这个脚本我可以看到在firebug test1.php文件执行(调用)3次,之后值写入控制台

as i said waht i want was
1. execute test1.php file
2. write value in console
3. give delay
then repeat

谢谢

我想你是在搜索这样的东西:

<script type="text/javascript">
function request(n) {
    $.ajax({
        type: "POST",
        url: "test1.php",
        cache:false,
        data:"id="+ msgids[n],
        dataType:'json',
        success: function(json)
        {
            var foo = json.foo;
            var uemail = foo.split(',');
            console.log(uemail);
            setTimeout(
                function() { 
                    request(n);
                }
                , 1000
            );
        }
    });
}
$(document).ready(function() {
    var listsValues="1,10,20";
    var n=0;
    var msgids = listsValues.split(',');
    $.each(msgids, function() {
        var html="TEST MESSAGE";
        n++;
        request(n);
    });
});
</script>