我有一個谷歌表,其中每 3 分鐘更新一次股票資料。我有一個頁面,可以在其中查看 html 表中的資料。但該資料不會自動更改。我必須重繪 頁面或者我必須設定元<meta http-equiv="refresh" content="30">來定期重繪 頁面。
我希望來自 JSON 的資料必須在沒有頁面重繪 的情況下自動更新。這是我的代碼。
JSON 物件鏈接
https://docs.google.com/spreadsheets/d/xxxxxxxxxx.json
div顯示json資料
<div id="json">json here</div>
用于獲取和顯示 json 資料的 Javascript
<script type="text/javascript">
var id = 'xxxxxxxxxxxxxxxxxxxx';
var gid = '0';
var url = 'https://docs.google.com/spreadsheets/d/' id '/gviz/tq?tqx=out:json&tq&gid=' gid;
fetch(url)
.then(response => response.text())
.then(data => document.getElementById("json").innerHTML=myItems(data.substring(47).slice(0, -2))
);
function myItems(jsonString){
var json = JSON.parse(jsonString);
var table = '<table><tr>'
json.table.cols.forEach(colonne => table = '<th>' colonne.label '</th>')
table = '</tr>'
json.table.rows.forEach(ligne => {
table = '<tr>'
ligne.c.forEach(cellule => {
try{var valeur = cellule.f ? cellule.f : cellule.v}
catch(e){var valeur = ''}
table = '<td>' valeur '</td>'
}
)
table = '</tr>'
}
)
table = '</table>'
return table
}
</script>
我知道一個 js 庫 kickout js,但我無法使用 kickout js 實作并獲得想要的結果。那么如何實作一個無需頁面重繪 即可自動更新 JSON 資料的表?
先感謝您。
uj5u.com熱心網友回復:
您必須使用 setInterval 定期獲取新資料并更新表行。您可以為每一行分配一個唯一 ID,并在定期運行的函式中更新它們。
setInterval(function() {
// fetch data here and update rows.
}, 180000);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327258.html
標籤:javascript php 查询 json
