下面的代码从数据库中动态生成 json 数据。生成数据后,它会将其添加到特定的 div。如何让 JSONObject 的内容每 30 秒重新加载一次。这将使内容显示近乎实时的变化。
<script>
JSONObject = <?php echo include_once('../includes/dashboard-stats.php'); ?>;
document.getElementById("today_visits").innerHTML=JSONObject.todayVisits;
</script>
下面是 JSONObject = <?php echo include_once('../includes/dashboard-stats.php'); ?>;
的输出
JSONObject = {"todayEarnings":"2.60","todayVisits":"212","todayClicks":"36","todayLeads":"3","todayCalculateCR":"12%","todayEPC":"0.08","todayCTR":"17%","yesterdayEarnings":"0.40","yesterdayClicks":"35","yesterdayVisits":"148","yesterdayLeads":"1","yesterdayCalculateCR":"35%","yesterdayEPC":"0.03","yesterdayCTR":"24%","monthEarnings":"3.00","monthClicks":"75","monthVisits":"392","monthLeads":"4","monthCalculateCR":"19%","monthEPC":"0.05","monthCTR":"19%"}
1;
我尝试使用它来尝试重新加载 json 数据。
<script>
function load(){
JSONObject = <?php echo include_once('../includes/dashboard-stats.php'); ?>
document.getElementById("today_visits").innerHTML=JSONObject.todayVisits;
setTimeout("load()",9000);
}
</script>
最佳答案
您的 PHP 代码仅运行一种类型,这就是为什么结果显示相同。 使用 Ajax 调用并每 30 秒通过 PHP FILE 从数据库中获取新数据。
//Jquery语法
$.post("Your PHP SCRIPT FILE PATH HERE", { PARAMS you want to pass }, function( Get DATA FROM PHP FILE ) {
// HERE IS YOU Operation
},"DATA FORMAT");
==================================
//代码示例
function loadStats(){
$.post( "../includes/dashboard-stats.php", { get:"stats" }, function(data) {
$("#today_visits").html(data.todayVisits);
}, "json");
}
$(function(){
loadStats();
setInterval(loadStats,9000);
}):
关于javascript - 如何重新加载 JSONObject 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23221353/