Skip to content
Advertisement

New data is being placed on top of old data (JavaScript)

I’m a newbie to the web development world, so please pardon me if I miss something here and there or if I describe the issue incorrectly.

GIF illustrating the issue

Basically, I copied different code from TradingView’s two or more chart examples into one JSFiddle and tried to make a chart that could show candlesticks of different time frames with the symbol name, OHLCV, MA10 values. Everything works fine when the chart loads for the first time but when click the button to change the time frame, all the candles and MA10 line loads fine but the OHLCV and MA10 values seem like being put on the old (initially loaded) data. When I tried it for the first time all the candles and MA10 lines were also overlapping then I figured that the whole chart has to be removed so that MA10’s values will be re-calculated. So now the candlesticks and MA10 are loading fine but OHLCV values are still overlapping on the previous values. Since I’m new to JS I can’t figure out the right keywords for my problem so I’m not getting results that address my problem. Could somebody help me to figure out what’s going wrong?

Thank you.

I had to remove most of historic data to comply with Stackoverflow’s limit of 30000 chars per post, so snippet below will not work propely. You can take a look at the code here https://jsfiddle.net/akshay7892/dhutrgfn/7/

JavaScript
JavaScript
JavaScript

Advertisement

Answer

First thing to look at when something’s not working as expected (whatever your level!) is the dev console! Here you can clearly see that each time you click on your button you’re not only replacing an “old” value but instead adding one on top of the previous one(s).

I would recommend having an init function and/or components and an update function.

Constantly adding divs

Advertisement