facebook

Where to add code for new array? CLOSED

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #348922 Reply

    Olly
    Member

    Hi,

    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

    #348924 Reply

    Code_A
    Member

    You can define it in your <screen>_custom.js file within your project folder. Have you tried doing it there?

    #348927 Reply

    Olly
    Member

    @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!

    #348930 Reply

    Code_A
    Member

    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.

    #348940 Reply

    Olly
    Member

    @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

    #348943 Reply

    Code_A
    Member

    Try this:

    var myArray = new Array();

    Or, the shorthand method:

    var myArray = [];
    #348946 Reply

    Olly
    Member

    @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

    #348947 Reply

    Code_A
    Member

    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.

    #348960 Reply

    Olly
    Member

    @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

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: Where to add code for new array? CLOSED

You must be logged in to post in the forum log in