0

I am having an util class which is not owned by me. I am calling one of the method of that util class which inturn calls one ajax function. And gives me response back.

I need to make a decision depending on the response object. The problem is that response object takes some time to populate(mili seconds).

var selector = dojo.byId("SelectorId");
var theChart = new chart( selector, 135, 92, style, frequency, time);

if(theChart.data ===null){
    console.log("No response");
}else{
    Console.log("Use response data");
}

and

chart( selector, 135, 92, style, frequency, time);

is not owned by me. chart() is from util class.

above snippet works fine with break point. But when I remove the breakpoint it starts going in "if" block always.

How can I solve this issue.

2 Answers 2

1

If you use setTimeout, it should solve your problem:

 var selector = dojo.byId("SelectorId");
 var theChart = new chart( selector, 135, 92, style, frequency, time);

 setTimeout(function(){
  if(theChart.data ===null){
   console.log("No response");
  }else{
   Console.log("Use response data");
  }
 }, 100);

Alternatively, you may use setInterval. The example below checks the variable avery 100msecs, and returns 'false' after N=10 attempts:

 var maxTries = 10;
 var checkFunction = function(){
  if(theChart.data === null){
   --maxTries;
  }else{
   Console.log("Use response data");
   clearTimeout(checkFunction);
   return;
  }
  if( 0 == maxTries ) {
   console.log("No response");
   clearTimeout(checkFunction);
  }
 } ;
 setTimeout(checkFunction, 100);
5
  • 100 is 1/10th of a second, so 1000 = 1 sec
    – St.Woland
    Dec 28, 2010 at 14:48
  • If the server takes longer than your timeout the else condition will never be executed. you might want to set another timeout if data is null (and another (and another)).
    – kioopi
    Dec 28, 2010 at 15:03
  • alternatively you may use setInterval instead of setTimeout, and clearInterval after certain time. See updated answer.
    – St.Woland
    Dec 28, 2010 at 15:19
  • @St.Woland. Thanks! Its working now with your first suggestion of setTimeOut. Now I have just one more question-Will the control always wait for 1/10 of second then it will go either in IF or ELSE condition? I got the ans of my question. Thanks for all your help
    – Rachel
    Dec 28, 2010 at 16:01
  • Yes, it will always wait, however I don't know whether the timeout of 0.1 second will always be sufficient for you. That's why you might want to try the 2nd option, to be on the safe side.
    – St.Woland
    Dec 29, 2010 at 6:22
1

The chart object should provide some kind of callback-hook to let you know when the data is available.

Something like

theChart.onData = function(data){ 
  console.log(data);
  // or 
  console.log(theChart.data); 
} 

if that is not available you'd have to use timeouts to buid the functionality. Since you can't know if the server might take some time to respond you might have to try repeatedly.

This (untested) function should call the inner function until data is available which will in turn call the provided callback-function.

function chartdata(ch,cb){ 
  (function(){
    if(ch.data === null){
      window.setTimeout(arguments.callee, 100);
    } else {
      cb(ch);     
    }
  })();
}; 

You'd use it like this:

chartdata(new chart( selector, 135, 92, style, frequency, time), function(theChart){  
  console.log(theChart.data);
});

You might want to add an abort-condition in case the server doesn't respond at all.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.