jquery 错误:必须在调用google.charts.setOnLoadCallback之前调用google.charts.load

jobtbby3  于 2023-06-22  发布在  jQuery
关注(0)|答案(2)|浏览(148)

我在HTML中有以下导入:

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

我的一些js代码:

// load google charts library
google.load("visualization", "1", {packages: ["corechart"]});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);

// for rest, walk, fast_walk data
var data, options, chart;

var i = 0;

/* initialize chart1 - rest, walk, fast_walk data */
function drawChart(data, options) {
    var chart = new google.visualization.LineChart(document.getElementById('data-container'));
    chart.draw(data, options);
    return(chart);
}

但是我得到错误:必须在调用google.charts.setOnLoadCallback之前调用google.charts.load
如何解决这个错误?谢谢你!

h43kikqp

h43kikqp1#

From:https://developers.google.com/chart/interactive/docs/release_notes
google.charts.load()调用现在返回一个JavaScript Promise,所以除了其他回调机制之外,你还可以像这样调用你的图表绘制函数:google.charts.load('current ',{packages:['corechart']}).then(drawChart);
而不是使用:

google.charts.load('current', {packages:['corechart']});

google.setOnLoadCallback(drawChart);

使用一次呼叫即可:

google.charts.load('current', {packages:['corechart']}).then(drawChart);
wqsoz72f

wqsoz72f2#

我通过删除以下代码解决了这个错误:

// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);

谢谢你!

相关问题