- This topic has 8 replies, 2 voices, and was last updated 10 years, 10 months ago by
Olly.
-
AuthorPosts
-
OllyMemberHi,
I’ve set a piece of code to run when a button is clicked which creates an array, then pushes data in. My problem is when I try to add a second piece of data, the array is reset.
Can I create the array on app start, and if so where? I’ve tried what seems logical to me with no luck.
Thank you
Code_AMemberYou can define it in your <screen>_custom.js file within your project folder. Have you tried doing it there?
OllyMember@Code A wrote:
You can define it in your <screen>_custom.js file within your project folder. Have you tried doing it there?
I’ve created a function in the custom.js file and the onclick action for the button then calls that function.
I’ve tried adding the create array variable in different parts of the custom.js file but again with no luck.
When adding the variable to the main.js file, still no joy!
Code_AMemberDid you try globally defining the array outside of the functions at the top of the _custom.js file? I believe this should work. I cannot test right now but I know I have successfully defined global variables using this method in the past.
OllyMember@Code A wrote:
Did you try globally defining the array outside of the functions at the top of the _custom.js file? I believe this should work. I cannot test right now but I know I have successfully defined global variables using this method in the past.
Oh no I haven’t tried that! How do you define it globally? Appreciate your help
Code_AMemberTry this:
var myArray = new Array();
Or, the shorthand method:
var myArray = [];
OllyMember@Code A wrote:
Try this:
var myArray = new Array();
Or, the shorthand method:
var myArray = [];
Yes! Thank you!
I have been defining my arrays globally but what solved the problem was by entering it at the top of the custom.js file outside of any functions. Before I asked for help I had tried adding it in other functions!
Problem solved. Thank you
Code_AMemberGood, I am glad to hear this solved your problem.
Anything variables defined outside of a function is considered to be “global” and can be accessed from any function within your code. Any variable that is defined withing a function is considered to be “local” and can only be accessed within that function. Once that function has finished executing, local variables are destroyed. This is why you were losing your data.
OllyMember@Code A wrote:
Good, I am glad to hear this solved your problem.
Anything variables defined outside of a function is considered to be “global” and can be accessed from any function within your code. Any variable that is defined withing a function is considered to be “local” and can only be accessed within that function. Once that function has finished executing, local variables are destroyed. This is why you were losing your data.
Ah ok that’s makes sense 🙂
I’m already onto the next problem but will give it a good go myself before I have to ask
-
AuthorPosts