[英]How do i ensure that i load my charts before i use them?
I want to load my google charts before I i run the rest of my javascript.我想在运行 javascript 的其余部分之前加载我的谷歌图表。
$(document).ready(function() {
google.charts.load("current", {packages:["corechart"]});
I originally put this code in to allow some time for the charts to load, but sometimes the charts take longer than expected and my code breaks.我最初放入此代码是为了让图表加载一些时间,但有时图表需要比预期更长的时间并且我的代码中断。 Is there an initializer function in javascript that I can use?
我可以使用 javascript 中的初始化函数吗?
setTimeout(function() {
map.setView([gon.locations[3].latitude, gon.locations[3].longitude - .01],15);
if(typeof lastMarker !== "undefined")
map.removeLayer(lastMarker);
$('progress').animate({value: gon.locations[3].overall_vulnerability}, {duration: 1000});
$('#vul').html(gon.locations[3].overall_vulnerability);
var markers = L.mapbox.featureLayer().setGeoJSON(getData(gon.locations[3])).addTo(map);
google.charts.setOnLoadCallback(drawChart(gon.locations[3]));
openNav();
lastMarker = markers;
}, 1500);
That is what the setOnLoadCallback()
function is for - it runs when the loading is finished.这就是
setOnLoadCallback()
函数的用途——它在加载完成时运行。
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(function() {
// Everything you want to happen after loading goes here
});
From the documentation :从文档:
Before you can use any of the packages loaded by google.charts.load you have to wait for the loading to finish.
在使用 google.charts.load 加载的任何包之前,您必须等待加载完成。 It is not enough to just wait for the document to finish loading.
仅仅等待文档完成加载是不够的。 Since it can take some time before this loading is finished, you need to register a callback function.
由于此加载完成可能需要一些时间,因此您需要注册一个回调函数。
you can also include the callback
in the load
statement...您还可以在
load
语句中包含callback
...
google.charts.load("current", {
callback: function () {
// google.charts and google.visualization now available
},
packages:["corechart"]
});
or use the promise the load
statement returns或使用
load
语句返回的承诺
google.charts.load("current", {
packages:["corechart"]
}).then(function () {
// google.charts and google.visualization now available
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.