doumeng3345 2010-04-09 07:22
浏览 113
已采纳

如何递归调用Ajax函数

I want to know how to call Ajax function Recursively. My ajax code is like this,

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  var step='1';
  $.ajax
  (

   {   
    url: 'test1.php',
    data: {step: step},        
    success: function(output) 
    {                                         
       step=eval(output);
       alert(step);
    }
   }
  );

</script>
</head>   
</html>

And php code is like this

<?php
 function writeName()
 {
  echo "step=2";
 }
 function ReadName()
 {
  echo "In Read Name Function";
 }
 if(isset($_REQUEST['step']) && !empty($_REQUEST['step']))
 {
  $step = $_REQUEST['step'];
  switch($step)
  {
   case '1' : 
    writeName();
    break;
   case '2' : 
    ReadName();
    break;
   default  : 
    echo "Empty String";   
  }
 }
?>

first time this function is get called with value of step variable equal to 1 and Function Write name modified it as 2. Now i want to call this Ajax function with Step variable value equal to 2. So that 2nd php function gets called.

  • 写回答

2条回答

  • duanpo1498 2010-04-09 07:27
    关注

    You can create a function that makes the Ajax call and accepts the step as an argument:

    function getStep(step) {
      $.ajax
        (
         {   
          url: 'test1.php',
          data: {step: step},        
          success: function(output) 
          {                                         
             step=parseInt(output);
             alert(step);
             if (step < 2) {
                getStep(step);
             }
          }
         }
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置