如何确保在使用图表之前先加载图表? [英] How do i ensure that i load my charts before i use them?

查看:50
本文介绍了如何确保在使用图表之前先加载图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行其余JavaScript之前先加载Google图表.

I want to load my google charts before I i run the rest of my javascript.

$(document).ready(function() {
google.charts.load("current", {packages:["corechart"]});

最初,我将这段代码放入其中,以便有一些时间加载图表,但是有时图表花费的时间比预期的长,并且我的代码中断了.我可以使用javascript中的初始化函数吗?

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?

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);

推荐答案

这就是 setOnLoadCallback()函数的作用-在加载完成时运行.

That is what the setOnLoadCallback() function is for - it runs when the loading is finished.

google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(function() {
    // Everything you want to happen after loading goes here
});

文档:

在使用google.charts.load加载的任何软件包之前,您必须等待加载完成.仅等待文档完成加载是不够的.由于此加载可能需要一些时间,因此您需要注册一个回调函数.

Before you can use any of the packages loaded by google.charts.load you have to wait for the loading to finish. 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.

这篇关于如何确保在使用图表之前先加载图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆