[英]Reload javascript variables with setinterval function
I am trying to learn Google Gauges with Google visualization and i want to display the data on the Gauge which changes every second and am loading the variable using a PHP command which assigns the variable as seconds value using shell_exec function, PFB my code 我正在尝试通过Google可视化学习Google Gauges,我想在Gauge上显示每秒更改的数据,并使用PHP命令加载该变量,该命令使用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);
after the function is being called after 1 second the value of memory
is not being refreshed and taken from parseFloat(<?php echo json_encode(shell_exec("date +%S")); ?>);
在1秒钟后调用该函数后,不会刷新
memory
的值并从parseFloat(<?php echo json_encode(shell_exec("date +%S")); ?>);
获取memory
parseFloat(<?php echo json_encode(shell_exec("date +%S")); ?>);
Please let me know how do i make the memory
variable reload everytime the function is called after 1 second 请让我知道如何在1秒后每次调用函数时重新加载
memory
变量
Thanks in advance 提前致谢
The current issue is that your php code is only executed once, the values are therefore not updated: <?php echo json_encode(shell_exec("date +%S")); ?>
当前的问题是您的php代码仅执行一次,因此值不会更新:
<?php echo json_encode(shell_exec("date +%S")); ?>
<?php echo json_encode(shell_exec("date +%S")); ?>
returns a single value, which is not updated periodically from your front end. <?php echo json_encode(shell_exec("date +%S")); ?>
返回一个值,该值不会从前端定期更新。
I would suggest you to use Javascript's Date
instead: 我建议您改用Javascript的
Date
:
var seconds = new Date() / 1000;
I have done that using two separate files, a PHP file to get the data from a database (in my case) and a javascript file that gets the response from the database using the PHP file and Ajax. 我使用两个单独的文件完成了此操作,一个PHP文件从数据库中获取数据(以我为例),另一个javascript文件使用PHP文件和Ajax从数据库中获取响应。
You have to include Ajax adding this to your index.html file: 您必须包括Ajax并将其添加到index.html文件中:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Javascript code: 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
Then in the google Gauge function do something like this: 然后在google Gauge函数中执行以下操作:
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);
}
In the PHP file: 在PHP文件中:
<?php
/* Get your data */
echo json_encode($table);
?>
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.