-1

I have a function which draws different popups depending on the value of my monitor type. If my monitorType = 22 I need to send an ajax request and depending on the response continue the drawing. But the problem is that I cant get response before to continue. Here is that part of my function:

var str = '<div id="thresholdsHeader" style="display:none;">';
            str += '<div id="alertRule"></div>';
            if(monitorTypeId == 22 ){                   
            var parametrs = {
                    monitorId: module.info.id,
                    typeId: module.info.monitorTypeId
                    };
                sendGetAPIRequest(Framework.customMonitorURL, "getMonitorDynamicMetrics", parametrs, ThresholdManager.getDynamicThresholdsRequest);             
                if(dynResponse.json.length == 0){
                    str += '<input type="button" designstyle="green" onclick="ThresholdManager.addThresholdDialog()" style="float:right;" value="'+Framework.Lang.addThreshold+'"/>';
                }else{
                    str +=  '<input type="button" id="trehsoldsGreenBut" designstyle="green" onclick="ThresholdManager.createThresholds(event)" style="float:right;" value="'+Framework.Lang.addThreshold+'"/><div class="dashboardIcons addNewRuleArrow" style="left: '+(CURRENT_LANGUAGE == 'en'? '460':(CURRENT_LANGUAGE == 'de'?'363':'355'))+'px;top: 8px;"></div>';
                }                   
            }else   {
                    str += '<input type="button" designstyle="green" onclick="ThresholdManager.addThresholdDialog()" style="float:right;" value="'+Framework.Lang.addThreshold+'"/>';
            }
            str += '</div><div id="thresholdContainer"></div>';

and this is my request handler :

 getDynamicThresholdsRequest: function(response){
            dynResponse = response;
     },

if I go slow by debugging it works ok.

Arno
  • 549
  • 1
  • 4
  • 22
  • whats the content of **sendGetAPIRequest** .. Cant you add a callback function as an additional parameter? – sabkaraja Oct 27 '15 at 07:16
  • thanks @Andreas, it is interesting, but I could not use it in my case. Im new in js – Arno Oct 27 '15 at 07:30

2 Answers2

0

Can you try like as follow:-

  var str = '<div id="thresholdsHeader" style="display:none;">';
            str += '<div id="alertRule"></div>';
            if(monitorTypeId == 22 ){                   
            var parametrs = {
                    monitorId: module.info.id,
                    typeId: module.info.monitorTypeId
                    };
                sendGetAPIRequest(Framework.customMonitorURL, "getMonitorDynamicMetrics", parametrs, ThresholdManager.getDynamicThresholdsRequest);    
           setTimeout( function(){        
                if(dynResponse.json.length == 0){
                    str += '<input type="button" designstyle="green" onclick="ThresholdManager.addThresholdDialog()" style="float:right;" value="'+Framework.Lang.addThreshold+'"/>';
                }else{
                    str +=  '<input type="button" id="trehsoldsGreenBut" designstyle="green" onclick="ThresholdManager.createThresholds(event)" style="float:right;" value="'+Framework.Lang.addThreshold+'"/><div class="dashboardIcons addNewRuleArrow" style="left: '+(CURRENT_LANGUAGE == 'en'? '460':(CURRENT_LANGUAGE == 'de'?'363':'355'))+'px;top: 8px;"></div>';
                }                   
            }else   {
                    str += '<input type="button" designstyle="green" onclick="ThresholdManager.addThresholdDialog()" style="float:right;" value="'+Framework.Lang.addThreshold+'"/>';
            }
            str += '</div><div id="thresholdContainer"></div>';},3000)

Use

SetTimeOut function

and give delay for execution.

Amit Soni
  • 3,216
  • 6
  • 31
  • 50
0

are you using jquery or something similar?

If not, what I would suggest it re-organising your code flow.

PS: I am doing a wild attempt with the finite information you have given.

sendGetAPIRequest(Framework.customMonitorURL, "getMonitorDynamicMetrics", parametrs, ThresholdManager.getDynamicThresholdsRequest, function(resp) {
   //add all the logic to be added on response here
});
sabkaraja
  • 342
  • 4
  • 15