2015-09-02 45 views
0

我叫每30秒javascript函数:有人可以解释这种奇怪的行为?

function getOutput() { 
sleep(30000); 
setInterval(function(){ 
getRequest(
     'prova1.php', // URL for the PHP file 
     drawOutput, // handle successful request 
     drawError // handle error 
); 
    return true; 
},30000); 

的prova1.php文件执行打开包含JSON格式的文本两页的命令,并将其保存在本地资源库,使用JSON文件的网页更新由C++程序大约每20秒:

<?php 

    exec(" wget http://127.0.0.1:8082/Canvases/Fe0_Cbc0_Calibration/root.json -O provami1.json"); 
    exec(" wget http://127.0.0.1:8082/Canvases/Fe0_Cbc1_Calibration/root.json -O provami2.json"); 

?> 

即happends的是,当我看到在第一时刻代码的执行脚本保存文件provami1.json和provami2.json与x的大小奇怪的千字节,但在第二个provami2.json变为0并且是空的。如果是最后一次运行,并且C++程序终止,则两个文件都有正确的数据。我真的不明白为什么它会发生。希望有人能帮忙。

+6

这里'C++'在哪里? –

回答

0

我认为这里的问题是,AJAX是异步的,所以你会返回true之前有任何来自请求的响应。

尝试调试这样:

function getOutput() { 
sleep(30000); 
setInterval(function(){ 
getRequest(
     'prova1.php', // URL for the PHP file 
     drawOutput, // handle successful request 
     drawError // handle error 
     // Also debug here the response from the request. 
); 
console.log('2'); 
    return true; 
},30000); 
0

检查用户有写权限或没有,并尝试使用wget与附加选项即wget的-o日志文件URL -a。 我认为如果您使用wget来检索数据,那么PHP可以选择这样做,您可以使用CURL或file_get_content来获取数据并写入文件。之后你的C++ prog会完成剩下的工作。希望这能帮到你

相关问题