简体   繁体   English

我如何确保在使用图表之前加载它们?

[英]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.

相关问题 如何确保每次从下拉列表中选择图表时都会加载图表? - How can I ensure that my charts diagram load every time I choose it from my dropdown? 我可以做些什么来确保每次从下拉列表中选择图表时重新加载图表? - What can I do to ensure that my charts diagram reload every time I choose it from my dropdown? 我如何确保我的asp.net验证程序在我致电客户端JavaScript之前触发 - How do i ensure my asp.net validators fire before i call client side javascript 如何在呈现模板之前使用流星subscriptionsReady()确保数据准备就绪? - How do I use meteor subscriptionsReady() to ensure data is ready before rendering template? 如何在页面重定向之前确保服务器请求已完成? - How do I ensure server request is complete before page redirect? 如何使用概率并将它们插入我的其他 function? - How do I use probability and insert them into my other function? 如何确保我的JS ERB对Turbolinks 5友好? - How do I ensure my JS ERB is Turbolinks 5 friendly? 如何确保 vuex 商店包含在我的构建中? - How do I ensure the vuex store gets included in my build? 如何加载用于网站的外部字体? - How do I load external font to use for my site? 如何将我的数据传递到谷歌图表API? - How do I pass my data into google charts api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM