0

I'm developing a web application (precisely it's a Tizen web application), I got that 'undefined' error although I already defined the callback function serviceConnectionCallback.

I'm new to javascript programming, hope you guys can help. Tks

Below is my code:

var serviceConnectionCallback = {
        onconnect : onconnectFromPhone ,
        onrequest : onRequest, 
        onerror : onerror
}

window.onload = function() {

    //do something...
    document.addEventListener("visibilitychange", pageVisibilityHandler, false);

};


function pageVisibilityHandler() {
  if (document.hidden) {
    console.log("hidden");
    /*!!!!!!!!!!!!! PROBLEM HERE !!!!!!!!!!!!!!!!!!!!!!!!*/
    webapis.sa.setServiceConnectionListener(serviceConnectionCallback);
  } else {
    console.log("visible");
  }
}
tomrozb
  • 25,773
  • 31
  • 101
  • 122
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126
  • 2
    `setServiceConnectionListener` is not defined. Maybe you have to wait for `webapis` to load? – Halcyon Jul 11 '14 at 18:38
  • 1
    That's great, ***!!! PROBLEM HERE !!!***, and you're showing us a function call that according to the error is undefined, and you're not showing us the function, how should we know why it's undefined when it's not even in the posted code ? – adeneo Jul 11 '14 at 18:43
  • Hi, I thought the 'undefined' is the callback `serviceConnectionCallback`, but like @Halcyon said above, maybe `webapis.sa` is not loaded yet. I'm trying to find out. – EyeQ Tech Jul 11 '14 at 18:48
  • If `serviceConnectionCallback` was undefined you'd see the error on the line it is called (inside `webapis` code). On the line OP indicates `setServiceConnectionListener` is being called so that is the undefined function. – Halcyon Jul 11 '14 at 18:54

1 Answers1

1

When are you calling pageVisibilityHandler()? The flow that I am using to reach setServiceConnectionListener is as follows, starting from a connect() method;

1. request an agent: 
       webapis.sa.requestSAAgent(onSuccess, onError);
2. set up the agents in onSuccess: 
       SAAgent = agents[0];
       SAAgent.setPeerAgentFindListener(peerAgentFindCallback); 
       SAAgent.findPeerAgents();
3. set up the listeners:
       SAAgent.setServiceConnectionListener(serviceConnectionCallback);
       SAAgent.requestServiceConnection(peerAgent);

If you are skipping step 2, your local variable SAAgent is never instantiated and you will get an undefined error. SAAgent is a local variable which you should have initialised as null if you followed the guides.

CodeMonkey
  • 1,426
  • 1
  • 14
  • 32