0

so currently I am building a website where I get data from google spreadsheets and then graph it on my website...

However, because this data is constantly streamed from somewhere else, I need to be able to show 10 data points on the graph every time. (So shows 10 data points then the next 10 and then the next 10 and so on). So I personally need 2 global variables to keep track of where in the data I am thus be able to present the user with the correct graph.

I have tried this...

<script>

var i = 0;
var lengtharray = 0; //This variable is the second global variable that I need and is updated within the same function as i.

function doSomething(){
    anotherFunction();
    i = i + 10;
    lengtharray++;

}

</script>

so the next time the script is run to show the next data point on the graph, the i value that I expect is the previous i value + 10 and the lengtharray value to be incremented...

instead, what I get is i = 0 again and lengtharray = 0 again..

I need these values to be "updated" and only initialised/reset when I want it to be/the browser closed and reopened on the webpage...

I hope this question all makes sense!

Thanks in advance.

Verglas
  • 79
  • 2
  • 10
  • Do you need to reset back the variables to 0 and 0? Did you try `var i;` and `var lenghtarray;` instead of initializing it globally? – Maihan Nijat Jan 11 '16 at 03:52
  • Use [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage) –  Jan 11 '16 at 04:00
  • dont "stream" by refreshing the browser window, its not '95 anymore. – LJᛃ Jan 11 '16 at 04:07
  • @TinyGiant that localStorage idea I have tried using just now, but I don't get how you can increment the value stored? I know you can set it but I can't seem to be able to keep that value for the next time round (so for the next set of data) – Verglas Jan 12 '16 at 03:26
  • @LJᛃ How else could I stream this data then as it is updated every 5~8 seconds in the google spreadsheet? – Verglas Jan 12 '16 at 03:26
  • `localStorage.item = 1` then to get it, you just access it the same way `localStorage.item` http://jsbin.com/rixururoto/edit?js,console –  Jan 12 '16 at 03:34
  • @TinyGiant if I do that if statement how would i know I'm checking for the correct item? so if I have 2 items then how would I know I'm checking the correct one? – Verglas Jan 12 '16 at 03:39
  • [ajax using the provided REST api](https://developers.google.com/apps-script/guides/rest/quickstart/js) – LJᛃ Jan 12 '16 at 03:40
  • You name the items like variables, I just used item as a name. Treat it like any other JavaScript object you would use. –  Jan 12 '16 at 16:26

1 Answers1

1

If the problem is due to those globals and the function being re-instantiated every time then you might just be able to use a singleton pattern to prevent this. If you are unfamiliar, it is a design pattern that always returns the previous instance of your class, variable, whatever. That way there is always only one instance of your object, class, variables etc. I have linked the implementation in javascript here

LBaelish
  • 649
  • 1
  • 8
  • 21