Callbacks

Hi all, I am having trouble with callbacks.  All of my attempts have not yielded the result I am after.  The code below isn't meant to work but is the concept of what I am trying to accomplish.  This is all done within the same watchface view file.  Can someone help me get the proper code to make this happen?

var myDataField1 = 0;

var myDataField1Fn = "fnData2()"

myDataField1 = method(:myDataField1Fn)  

//myDataField1 should now have the value of 2

hidden function fnData1() {
   return 1;
}
hidden function fnData2() {
   return 2;
}
Thank you,
T2D
  • Hello

    Here an example

    var VMode1224=0;      
    var Mode1224=[
    :Mode12H,
    :Mode24H
    ] ;

    var hh;
    var mm;
    var ss;
    var ampm="";
    var widthdiv2;
    var heightdiv2;

    function onLayout(dc) {
       //system.println("onLayout");
     
            widthdiv2=dc.getWidth()/2;
            heightdiv2=dc.getHeight()/2

                    if (System.getDeviceSettings().is24Hour){VMode1224=1; }
    }


    function Mode12H(){

                    hh =  (calculateAmPmHour(infoshort.hour)).format("%02d") ;
                    ampm = (infoshort.hour < 12) ? "a" : "p";
     
    }

    function Mode24H(){
           
     
                     hh =  infoshort.hour.format("%02d") ;
     
     

    }


    function calculateAmPmHour(hour) {
        hour = hour % 12;
        hour = (hour == 0) ? 12 : hour;

    return hour;
    }



     function onUpdate(dc) {
            method(Mode1224[VMode1224]).invoke(); // 
            System.println("HH="+hh+ " MM="+mm);
     }
  • myDataField1 = method(:fnData2);

  • Thank you for the help.  I was able to take your example and flocsy's to figure it out.  Long story short, I tried a hundred different things and the problem ended up being my functions where all hidden functions rather than functions.  

    Here is my final code that worked for me.

    var myDataField1 = 0;

    var myDataField1Fn = :fnData2;

    myDataField1 = method(myDataField1Fn).invoke();  

    //myDataField1 should now have the value of 2

    function fnData1() {
       return 1;
    }
    function fnData2() {
       return 2;
    }
  • Thank you for the help.  I was able to take your example and riedid74's to figure it out.  Long story short, I tried a hundred different things and the problem ended up being my functions where all hidden functions rather than functions.  

    Here is my final code that worked for me.

    var myDataField1 = 0;

    var myDataField1Fn = :fnData2;

    myDataField1 = method(myDataField1Fn).invoke();  

    //myDataField1 should now have the value of 2

    function fnData1() {
       return 1;
    }
    function fnData2() {
       return 2;
    }
  • I have been working on implementing and have a performance/efficiency question.

    In my onupdate I am planning on setting the variable up based on the watchface options the user has picked.  The question is to just pass the function name or the method.  One thing with the method is in the memory viewer it calls out a circular reference however in vscode itself it does not call out any circular references during the running of the watch face.

    Is there on efficiency difference between carrying the defined object and building the object each time?

    onupdate()

    myDataField1Fn = :fnData2;

    or

    myDataField1Fn = method(:fnData2);

    then in the main code

    myDataField1 = method(myDataField1Fn).invoke();  

    or 

    myDataField1 = myDataField1Fn.invoke();  

  • It's more efficient to only calculate it once and keep the value in the memory.

  • K, so that would be better to keep this in memory, myDataField1 = method(myDataField1Fn).invoke(); , and then myDataField1Fn.invoke();  each time the watchface updates....