How define global array in JavaScript inline?

Using Javascript from SpiderBasic
User avatar
Caronte3D
Posts: 187
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

How define global array in JavaScript inline?

Post by Caronte3D »

I have a timer procedure where I need to use (and keep alive) an array, but I don't know how to define it globally with inline :?
If I do: let positions = []; inside the procedure, no error, but the array is empty each time te execution enters the Procedure, if I try to define it outside Procedure, an error tell me: Uncaught ReferenceError: positions is not defined :(

I tried to put the declaration inside an if to only do in one time, but SpiderBasic think it's not initialized
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How define global array in JavaScript inline?

Post by Peter »

Can you create a small example code that illustrates your problem?
User avatar
Caronte3D
Posts: 187
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: How define global array in JavaScript inline?

Post by Caronte3D »

Code: Select all


Procedure Timer()
  
    Define arraySize
    !let positions = [];
    
    !const currentValue = { one: 10.00, two: 20.00 };
    
    !positions.push(currentValue);
    
    !v_arraysize=positions.length;
    
    Debug arraySize
    
EndProcedure  
  
   AddTimer(0, 1000)
   BindEvent(#PB_Event_Timer, @Timer())
 
User avatar
Caronte3D
Posts: 187
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: How define global array in JavaScript inline?

Post by Caronte3D »

I need !let positions = []; to be global, to not erase it each time it enters in Time procedure
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How define global array in JavaScript inline?

Post by Peter »

Code: Select all

! positions = [];

Procedure Timer()
  
  Define arraySize
  
  !const currentValue = { one: 10.00, two: 20.00 };
  
  !positions.push(currentValue);
  
  !v_arraysize=positions.length;
  
  Debug arraySize
  
EndProcedure  

AddTimer(0, 1000)
BindEvent(#PB_Event_Timer, @Timer())
User avatar
Caronte3D
Posts: 187
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: How define global array in JavaScript inline?

Post by Caronte3D »

:shock: :lol: I was trying with LET and VAR but I didn't know it was possible without anything (my JavaScript is near zero)
Thanks Peter ;)
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How define global array in JavaScript inline?

Post by Peter »

Caronte3D wrote: Wed Sep 25, 2024 12:42 pmI was trying with LET and VAR
I tried that first too. ;)

JavaScript is a very flexible, but also dangerous language. :mrgreen:
Post Reply