I am trying to add google trend chart on a html document. Problem is I have to grab on click keyword and pass it to google trend function so it can return data as clicked keyword. But for some reason when i call google trend function (named- TIMESERIES) inside .click scope it only load the chart not whole html so i have to call this function from outside .click scope. The reason is timing issue. Please have a look solution of this kind of timing issue solution here I cant implement this solution with my code. If you can then it really help me. Thanks in advance
<script>
function TIMESERIES(categoryName) {
return trends.embed.renderExploreWidget("TIMESERIES", {
"comparisonItem": [{
"keyword": categoryName,
"geo": "",
"time": "today 12-m"
}],
"category": 0,
"property": ""
}, {
"exploreQuery": "q=arts&date=today 12-m",
"guestPath": "https://trends.google.co.in:443/trends/embed/"
});
}
var categoryName = "";
$(".SearchMainCategory, .SearchSubCategory").click(function() {
categoryName = $(this).find("#Level1CatsName").val();
if (typeof categoryName === "undefined") {
categoryName = $(this).find("#Level2CatsName").val();
}
// TIMESERIES(categoryName) if i call this function from where then only this function (google trend chart actually) loads and page other contents not loading. so i need to call from outside then it works fine but i cant access "categoryName" variable from ourside.
});
TIMESERIES(categoryName);
</script>