简体   繁体   中英

How to append to an array every minute on every reload of page in Javascript?

I have a HTML code which is taking stock price which is updating every 1 minute ( on reload of page ). I want to make a graph out of the stock price. For this I would want to make an array in the morning and then as and when prices are updated every minute, the array is updated too. How can I do it? I tried to use local storage but just unable to get the desired results.

vwap_ltp_percentage is the variable here which I am getting every minute and which I want to append to an array

    vwap_ltp_array = Array()
    vwap_ltp_array.push(vwap_ltp_percentage)
    console.log(vwap_ltp_array)
    if (localStorage.getItem('test') == null) {
        localStorage.setItem('test',[])
    }
    else {
        localStorage.setItem('test',[])
    }
    localStorage.setItem("test", JSON.stringify(vwap_ltp_array))
    console.log(localStorage)

ge)

In your case, you are always starting with an empty array, the if...else construct is not doing anything... Instead, you need to construct the initial array from the storage if an entry exists there.

    var testString = localStorage.getItem('test');
    vwap_ltp_array = testString ? JSON.parse(testString) : []
    vwap_ltp_array.push(vwap_ltp_percentage)
    localStorage.setItem("test", JSON.stringify(vwap_ltp_array))
    console.log(localStorage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM