• Hi!

    each time I long press BTN3 all variables are lost. Is this expected ?

    Yes - because the device is a bit limited we decided that in order to make it as robust as possible we'd just reload everything each time, but that does mean state is lost.

    Q: Is there any other way to preserve a widget's state between long presses of BTN3,
    apart from writing to storage ?

    No, you'd have to use Storage, but if your value is changing state often and you don't want to write it all the time you can use the kill event to save the state of the value only when the Bangle is about to shut down: http://www.espruino.com/Reference#l_E_ki­ll

    var state = require("Storage").readJSON("myapp.json"­,1);
    if (state===undefined) state = { something: true };
    E.on('kill', function() {
      require("Storage").writeJSON("myapp.json­", state);
    });
    

    The only times it won't save state are when it shuts down unexpectedly (battery dies, 2 button reset, or actual firmware crash).

About

Avatar for Gordon @Gordon started