0

I have this code that uses a cordov/phonegap plugin:

var app = { 
   init: function (){
     sensors.getState(app.callback);
   },
   callback: function(state){
     //parameter of this callback function is define by cordova plugin 
     if (state[0] == 0) {
       //execute another function passed as argument because I wwant to change it in other parts of my code
       }
   },
   firstFunction(){
     //execute action 1
     },
   secondFunction(){
    //execute action 2
    }
  
}

Now sensors.getState has one argument and is predefined by plugin . I would like to execute a function passed as added argument like: sensor.getState(app.callback(firstFunction)) or when i need sensor.getState(app.callback(secondFunction))

Thanks. IngD

1 Answer 1

1

Try this

    var app = {
    init: function() {
        sensors.getState(app.callback);
    },
    test: function() {},
    setCallback: function(callback) {
        app.test = callback;
    },
    callback: function(state) {
        app.test();
    },
    firstFunction() {
        alert("firstFunction");
    },
    secondFunction() {
        alert("secondFunction");
    }
};
app.setCallback(app.firstFunction);
app.callback();
app.setCallback(function () { alert("thirdFunction"); });
app.callback();
Sign up to request clarification or add additional context in comments.

1 Comment

So in use your Init would look something like init: function() { sensors.getState(app.callback); app.setCallback(somethinghere); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.