[英]Can't use Variable Inside Async Function?
So, I know this must be a pretty dumb question but am not great in javascript, so I'm trying to insert Tradingview free widget on my site but the problem is I can't use the variable inside it because of its async function.所以,我知道这一定是一个非常愚蠢的问题,但在 javascript 中并不是很好,所以我试图在我的网站上插入 Tradingview 免费小部件,但问题是我不能在其中使用变量,因为它是异步的 function。
here is code这是代码
<script>var coinName = "XRP"</script>
so I'm using 2 scripts from Tradingview, first one is this and its working perfectly fine.所以我使用了 Tradingview 的 2 个脚本,第一个就是这个,它工作得很好。
<div class="tradingview-widget-container">
<div id="tradingview_8e4c9"></div>
<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
<script type="text/javascript">
new TradingView.widget(
{
"width": 980,
"height": 610,
"symbol": "BINANCE:" + coinName + "USD",
"interval": "D",
"timezone": "Etc/UTC",
"theme": "dark",
"style": "1",
"locale": "en",
"toolbar_bg": "#f1f3f6",
"enable_publishing": false,
"allow_symbol_change": true,
"container_id": "tradingview_8e4c9"
}
);
</script>
</div>
this is the second code, I'm trying to do exactly same as the script above but its not working这是第二个代码,我正在尝试与上面的脚本完全相同,但它不起作用
<!-- TradingView Widget BEGIN -->
<div class="tradingview-widget-container">
<div class="tradingview-widget-container__widget"></div>
<div class="tradingview-widget-copyright"><a href="https://www.tradingview.com/symbols/BTCUSD/technicals/" rel="noopener" target="_blank"><span class="blue-text">Technical Analysis for BTCUSD</span></a> by TradingView</div>
<script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-technical-analysis.js" async>
{
"interval": "1M",
"width": 425,
"isTransparent": false,
"height": 450,
"symbol": "BINANCE:" + coinName + "USD",
"showIntervalTabs": true,
"locale": "en",
"colorTheme": "dark",
"largeChartUrl": "http://#"
}
</script>
</div>
<!-- TradingView Widget END -->
second code gives 2 error in console,第二个代码在控制台中给出 2 个错误,
Widget settings parse error: SyntaxError: Unexpected token + in JSON at position 105小部件设置解析错误:SyntaxError: Unexpected token + in JSON at position 105
and和
Invalid settings provided, fall back to defaults提供的设置无效,回退到默认值
so how can I achieve that?那么我该如何实现呢?
@CrisG s answer solved this issue @CrisG 的回答解决了这个问题
Got it to work: jsfiddle.net/khrismuc/3s5rn1bg
让它工作:jsfiddle.net/khrismuc/3s5rn1bg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.