我正在尝试制作一个图表,在页面上显示 CPU 百分比,并随着 CPU 百分比的变化自动更新。在发送 AJAX 请求后,我无法理解如何从 PHP 脚本中获取信息。理想情况下,我希望能够使用变量 '$cpuPercent' 并在 '.update();' 我的更新功能的一部分。
我尝试在更新函数中使用 php 代码并回显 $cpuPercent,但它不会每次都更新,因为它只会运行一次,并且每次更新时都使用相同的数字。
这是我的 system_actions.php 脚本中的 php。
exec('wmic cpu get loadpercentage', $output2);
$cpuPercent = $output2[1];
echo json_encode($cpuPercent);
这是我的 javascript
function getUsage(){
$.ajax({
url:'system_actions.php?action=cpu',
async: true,
dataType: 'json',
type: 'post',
success:function(output){
var cpuPercent = parseInt(output);
document.getElementById('cpu').value = cpuPercent;
updateChart();
}
})
}
setInterval(getUsage, 2500);
function updateChart(){
var link = document.getElementById('randombutton');
link.click();
}
$('.updatePieCharts').on('click', function(e) {
e.preventDefault();
charts.each(function() {
var number = parseInt(document.getElementById('cpu').value);
$(this).data('easyPieChart').update(number);
});
});
我肯定觉得这一切是如何协同工作的,但我无法完全弄清楚。任何帮助或正确方向的观点都会很棒。回到谷歌我去。