我正在尝试使用Google可视化来学习Google计量器,我想在计量器上显示数据,它每秒钟变化一次,并使用一个使用shell_exec函数将变量赋值为秒的命令加载变量,PFB是我的代码。
setInterval(function() {
var memory = parseFloat(<?php echo json_encode(shell_exec("date +%S")); ?>);
data.setValue(0, 1, memory);
chart.draw(data, options);
}, 1000);
在1秒后调用该函数之后,memory
的值将不会刷新并从parseFloat(<?php echo json_encode(shell_exec("date +%S")); ?>);
中获取。
请告诉我如何使memory
变量在1秒后被调用时重新加载。
提前感谢
发布于 2017-02-08 22:34:12
我使用了两个单独的文件,一个PHP文件从数据库中获取数据(在我的例子中),一个javascript文件使用PHP文件和Ajax从数据库获得响应。
您必须将Ajax添加到您的index.html文件中:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Javascript代码:
var mysqlData; // Global variable in witch the data will be stored
setInterval(function() {
$.ajax({
url: "php/getdata.php", // file name or path to PHP file
dataType: "JSON",
data:{},
success: function(x){
mysqlData = x; // Assign the response to global variable mysqlData
}
});
}, 2000); // Run every 2 seconds
然后,在google的量程函数中,做这样的事情:
function SOC() {
// Normal google chart code goes here
setInterval(function() {
data.setValue(0, 1, mysqlData["SOC"] ); // In my case mysqlData is an object which contains the key "SOC"
formatter.format(data, 1); // Only needs to be done if google.visualization.NumberFormat have been used
chart.draw(data, options); // To redraw the gauge with new data
}, 2000);
}
在PHP文件中:
<?php
/* Get your data */
echo json_encode($table);
?>
希望能帮上忙!
发布于 2017-01-25 14:56:14
当前的问题是您的php代码只执行一次,因此这些值不会被更新:<?php echo json_encode(shell_exec("date +%S")); ?>
返回一个值,这个值不会从前端定期更新。
我建议您使用Javascript的Date
代替:
var seconds = new Date() / 1000;
https://stackoverflow.com/questions/41854618
复制相似问题