0

With the given code -

var videowallwidth;
var videowallheight;
    function getPositions() {
   $.ajax({
       url: 'https://' + ipaddress + ':46272/NetAPICmd?Command=Output',
       type: 'get',
       data: '[]',
       dataType: 'jsonp',
       success: function (response1) {
           var msg1 = response1.Result;
           var testarray1 = msg1.split("\r\n");
           for (var i = 1; i <= testarray1.length - 1; i++) {
               var res = testarray1[i].split(':');
               var newRes = res;
               if (newRes[0] == "TotalSize") {
                   var ressplit = newRes[1].split('x');
                   videowallwidth = ressplit[0].trim(); 
                   videowallheight = ressplit[1].trim(); 
                  
                }
            }       
        }
    });
}

console.log(videowallwidth);

I have defined videowallwidth and videowallheight as global variables and set their values inside getPositions() function, When i console log the variables inside the function they seem fine and have the right values. But when i called them outside the function they show "undefined" which means their values didnt update globally... How do i fix this??

CodingNoob
  • 65
  • 6

2 Answers2

0

The callback function is asynchronous and you will get the correct value once the process is fully completed. To get the correct value, you need to log value inside the callback function.

 function getPositions() {
   $.ajax({
       url: 'https://' + ipaddress + ':46272/NetAPICmd?Command=Output',
       type: 'get',
       data: '[]',
       dataType: 'jsonp',
       success: function (response1) {
           var msg1 = response1.Result;
           var testarray1 = msg1.split("\r\n");
           for (var i = 1; i <= testarray1.length - 1; i++) {
               var res = testarray1[i].split(':');
               var newRes = res;
               if (newRes[0] == "TotalSize") {
                  var ressplit = newRes[1].split('x');
                  videowallwidth = ressplit[0].trim(); 
                  videowallheight = ressplit[1].trim(); 
                  console.log(videowallwidth, videowallheight);  // <------------------
               }
            }       
        }
    });
}
Prime
  • 2,809
  • 1
  • 7
  • 23
  • I understand what your saying but loggin inside doesnt do me any good i want to make the variables values accessible to other functions and not to only getPositions – CodingNoob Dec 28 '20 at 07:38
0

You might want to insert global variables inside the window object, like this:

window.videowallwidth = null;  //initialize as you please
window.videowallheight = null;  //initialize as you please

Like this, you should be sure they're reachable everywhere

Watch out with ajax, those functions are asynchronous, so you should use callbacks/promises etc.. to manage the "flow"

Luca Corsini
  • 738
  • 4
  • 20
  • I Tried setting it globally through 'window.variableName' and also tried setting the values inside the functions with window['variableName'] but doesnt seem to work. The variables are reachable, but they arent updated globally through the function – CodingNoob Dec 28 '20 at 07:59
  • wait: try to initialize them with a valid value, like 0. Are you still getting "undefined"? – Luca Corsini Dec 28 '20 at 08:00
  • No, if i initialise them globally, i am getting the global value so if its 0, the output i get is 0 and not the values that are updated inside the function.... – CodingNoob Dec 28 '20 at 08:03
  • Try to stop with a debugger after the line videowallheight = ressplit[1].trim(); Go into the debugger console, write window.videowallwidth; and see what it says. – Luca Corsini Dec 28 '20 at 08:06
  • Did it so the value of the variable changes inside the function but remains 0 outside the function – CodingNoob Dec 28 '20 at 08:13