Javascript localStorage to avoid a Chart's loss on webpage refresh

Viewed 15

I have a webpage that is presenting chart. I need that when a refresh occurs, the data printed on the web page's chart will not be erased. I looked up and found I should use local storage to not to loose the latest printed data. When I refresh the webpage, I loose the latest printed Chart's CandleBar.

if(localStorage.getItem("autoSaveStorage")){
        const getAutoSaveData = JSON.parse(localStorage.getItem('autoSaveStorage'));
        getAutoSaveData.charts[0].panes[0].sources[0].state.symbol = Datafeed.urlParameter.fullname;
        tvWidget.load(getAutoSaveData);
        console.log(getAutoSaveData);
        tvWidget.load(JSON.parse(localStorage.getItem('autoSaveStorage')));
        document.getElementById('tradVuTitle').innerHTML =  Datafeed.urlParameter.symbol + " " + localStorage.getItem("lastClosedPrice");
    }



    tvWidget.subscribe("onAutoSaveNeeded", function(res){
        tvWidget.save(function(res){
            localStorage.setItem('autoSaveStorage',JSON.stringify(res));
            console.log(JSON.stringify(res));
        });
    });

I only need it to be saved upon refresh, is there a way to handle this immediately before the refresh? Or should I continuously update the local storage through each change in my properties and variables?

0 Answers
Related