按顺序延迟一段时间后在jquery中执行php文件

时间:2022-04-21 01:17:19

i have one jquery function which i need to work in following way

我有一个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

for this purpose i write bellow jquery and php file

为此,我写下了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> 

and here is test1.php

这是test1.php

<?php
$id=$_POST['id'];

$val="";
for($i=1;$i<=$id;$i++) { 

$val.=$i;

}

$return["foo"] =$val;

print stripslashes(json_encode($return));

?>

but this is not working as a way i want, when i execute this script i can see in firebug test1.php file executes (called) for 3times and after that values are written in console

但这不是我想要的方式,当我执行这个脚本时,我可以在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

Thanks

谢谢

1 个解决方案

#1


1  

I think you are searching something like this:

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

<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>

#1


1  

I think you are searching something like this:

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

<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>