weixin_33724659 2013-05-22 11:52 采纳率: 0%
浏览 29

为什么需要多次调用Ajax?

我需要多次调用ajax才能写入linux中的命名管道,下面是我的代码。

Javascript和Ajax:

var count = 0;
var status = "online";

while(status=="online" && count!=25){

$.ajax({ url: 'http://192.168.5.10/Command.php',
         data: {cmd: 'GORIGHT'},
         type: 'post',
         success: function(data) {
                      console.log(data);
                  }
});

count++;
}

Command.php

if($_POST['cmd']==="GORIGHT"){

$fd = fopen("/tmp/myFIFO","w");
fwrite($fd, "GORIGHT
");
fclose($fd);
echo "SUCCESS";
}

这是正确的方法吗? 还是有更快的方法?会造成延迟吗?

  • 写回答

1条回答 默认 最新

  • 旧行李 2013-05-22 12:04
    关注

    If you need it to be synchronous, you will need to make sure that every calls are waiting completion of the previous one. Something like this should do the trick, untested thought:

    var count=0;
    var N=25;
    loop();
    
    function loop() {
        $.ajax({ url: 'http://192.168.5.10/Command.php',
            data: {cmd: 'GORIGHT'},
            type: 'post',
            success: function(data) {
                console.log(data);
            }
        })
        .done(function(data){
            if (count < N) {
                count++;
                loop();
            }
        });
    }
    

    Another idea (faster) could be to add an extra parameter in Command.php to allow you to specify how many time a predefined action must be done. You could have something like this instead:

    var N=25;
    
    $.ajax({ url: 'http://192.168.5.10/Command.php',
        data: {cmd: 'GORIGHT', repeat:N},
        type: 'post',
        success: function(data) {
            console.log(data);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化