2016-12-04 49 views
-1

我有以下代碼如何用獲取請求重新加載可數據客戶端?

$(document).ready(function(){ 
    var url = "https://query.yahooapis.com/v1/public/yql?format=json&q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDEUR%22,%20%22USDJPY%22,%20%22USDBGN%22,%20%22USDCZK%22,%20%22USDDKK%22,%20%22USDGBP%22,%20%22USDHUF%22,%20%22USDPLN%22,%20%22USDRON%22,%20%22USDSEK%22,%20%22USDCHF%22,%20%22USDNOK%22,%20%22USDHRK%22,%20%22USDRUB%22,%20%22USDTRY%22,%20%22USDAUD%22,%20%22USDBRL%22,%20%22USDCAD%22,%20%22USDCNY%22,%20%22USDHKD%22,%20%22USDIDR%22,%20%22USDILS%22,%20%22USDINR%22,%20%22USDKRW%22,%20%22USDMXN%22,%20%22USDMYR%22,%20%22USDNZD%22,%20%22USDPHP%22,%20%22USDSGD%22,%20%22USDTHB%22,%20%22USDZAR%22,%20%22USDISK%22)&env=store://datatables.org/alltableswithkeys"; 
    $.get(url, function(data) { 
    var ratesJSON = data['query']['results']['rate']; 
    console.log("Data Loaded: " + data['query']['results']['rate'].length); 
    $('#example').DataTable({ 
     data: ratesJSON, 
     columns: [ 
      { data: "Name" }, 
      { data: "Rate" }, 
      { data: "Ask" }, 
      { data: "Bid." }, 
      { data: "Date" }, 
      { data: "Time" } 
     ] 

    }); 
}); 

}); 

上述工作就好了。但是現在我需要每30秒重新載入表格(以及數據)。我可以使用數據表重新加載來實現它嗎?或者我應該使用jQuery重複整個過程?

+0

「什麼是最好的方式」過於寬泛。你有什麼想法? datatables是一個大型javascript框架的模塊;我相信你可以谷歌的東西了。 –

+0

我可以只使用jquery並重復整個過程嗎?或者可能使用數據表重新載入並重構代碼? – abiieez

回答

0

我得到它的工作通過重組代碼到

$(document).ready(function(){ 
    var table = $('#example').DataTable({ 
     //data: ratesJSON, 
     columns: [ 
      { data: "Name" }, 
      { data: "Rate" }, 
      { data: "Ask" }, 
      { data: "Bid." }, 
      { data: "Date" }, 
      { data: "Time" } 
     ], 
     ajax : { 
      url: "http://query.yahooapis.com/v1/public/yql?format=json&q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDEUR%22,%20%22USDJPY%22,%20%22USDBGN%22,%20%22USDCZK%22,%20%22USDDKK%22,%20%22USDGBP%22,%20%22USDHUF%22,%20%22USDPLN%22,%20%22USDRON%22,%20%22USDSEK%22,%20%22USDCHF%22,%20%22USDNOK%22,%20%22USDHRK%22,%20%22USDRUB%22,%20%22USDTRY%22,%20%22USDAUD%22,%20%22USDBRL%22,%20%22USDCAD%22,%20%22USDCNY%22,%20%22USDHKD%22,%20%22USDIDR%22,%20%22USDILS%22,%20%22USDINR%22,%20%22USDKRW%22,%20%22USDMXN%22,%20%22USDMYR%22,%20%22USDNZD%22,%20%22USDPHP%22,%20%22USDSGD%22,%20%22USDTHB%22,%20%22USDZAR%22,%20%22USDISK%22)&env=store://datatables.org/alltableswithkeys", 
      dataSrc: function (json) { 
        return json['query']['results']['rate']; 
      } 
     } 
    }); 

    setInterval(function() { 
    table.ajax.reload(); 
    }, 3000); 

}); 
相關問題