Home forums Using CanvasJS Can not use variables outside the function Reply To: Can not use variables outside the function

#24230

@sepetar,

Variable S and A gets value and goes to global scope inside updateChart method, which gets executed after 10ms (based on the value of updateInterval). But you are consoling the values is outside the method – which gets executed first. Consoling S and A after the first execution of updateChart method should work fine in this case.

Example:

var updateChart = function () {
    S = 35;
    A = Math.sin(Xa * Math.PI / 180);
}
setInterval(function(){
    updateChart();
    consoleValues();
}, 1000);

function consoleValues(){
  console.log(S); //Still accessible outside updateChart method
  console.log(A);
}


Vishwas R
Team CanvasJS