-
• #2
Hi, what you're doing is fine, however you may not have to set your global variables on
onInit()
...- If you declare a global variable, it will be saved (along with its value) just like a function or object definition would be - so declaring them is absolutely fine.
- Also, any
setInterval
orsetWatch
that was created will be remembered onInit()
will just be called once by the system at startup, after a hard reset or a call toload
orsave
.reset
will reset the device but won't load the saved code (so won't executeonInit
).
If you just send your code over to the device then you'll have to call
onInit
. However if you send your code to the device and then typesave()
,onInit
will be called automatically - If you declare a global variable, it will be saved (along with its value) just like a function or object definition would be - so declaring them is absolutely fine.
-
• #3
So the habit / pattern I developed now is this
- change code in the IDE, transfer it over
- in the console, call save() - then it is saved for "standalone" and I don't have to call init.
--> so reset() - I would do this to get a "blank" espruino to play with. But it would not clear the persistent memory, so after a power on/off or reset button press, it would be back as it loads again.
Thx!
- change code in the IDE, transfer it over
-
• #4
Yes, that's it...
Hi all,
I have trouble to understand when I would call save() load() and reset(). I've declared a few global variables at the top of my espruino code:
var channel, led2State, debug;
These variables are initialized in an onInit() function, that I typically call manually once I have moved the code over to the device. The onInit() function is called once the device starts up - e.g. after a reset button click, right?
Now, I could also make the onIinit() call at the bottom of the code - once I flash the code over, it would execute the onInit() once, - just like after a reset. But a reset - if I understand correctly - will not execute the onInit() twice, as the call to onInit() is done by the system.
Are these assumptions correct?
Is it OK to declare global variables -what happens to these once I do a save()?
Thx
Sven