For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub
- This topic has 8 replies, 2 voices, and was last updated 11 years, 7 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
April 22, 2014 at 5:51 am #348924
Code_AMemberYou can define it in your <screen>_custom.js file within your project folder. Have you tried doing it there?
April 22, 2014 at 6:16 am #348927
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!
April 22, 2014 at 7:22 am #348930
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.
April 22, 2014 at 11:07 am #348940
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
April 22, 2014 at 12:13 pm #348943
Code_AMemberTry this:
var myArray = new Array();Or, the shorthand method:
var myArray = [];April 22, 2014 at 1:06 pm #348946
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
April 22, 2014 at 1:25 pm #348947
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.
April 23, 2014 at 6:47 am #348960
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
