开发者

execute php file in jquery after a time delay in sequence

开发者 https://www.devze.com 2023-03-25 09:14 出处:网络
i have one jquery function which i need to work in following way 1. call a php file 2. log the values return by php file

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

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

<script type="text/javascript">
$(document).ready(function() {
listsValues="1,10,20";
n=0;
msgids = listsValues.split(',');
$.each(msgids, function() {
html="TEST MESSAGE";
 n++;
setTimeout开发者_StackOverflow中文版(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

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

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

then repeat

Thanks


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

精彩评论

暂无评论...
验证码 换一张
取 消