I want the variable to be accessed from anywhere

66 views Asked by At

Here i want to access the variable "finalPrice " in another function, how can i achive this?

  function available(id){
    $.ajax({
        method:"GET",           
    }).done(function(data){        

     for(j=0; j<available_location.length; j++) {

            var finalPrice =available_location[j].price_details.final_price;
   };
});

function razorpay() {      
    $.ajax({
        url : 'http://test4238.wickedride.com//api/cancelledEnquiries/add',
        type : 'GET',
        data : {

         },
        success: function(data){
              alert(finalPrice );
           }
    });
}
3

There are 3 answers

0
Koby Douek On BEST ANSWER

Your finalPrice is scoped only to the available function since that's where it's declared (var finalPrice). In Javascript, if you want a variable to be accessable from all script scopes, move it outside the function, to the beginning of the script:

var finalPrice;

function available(id) {
    $.ajax({
          method:"GET",           
    }).done(function(data) {        
       for(j = 0; j < available_location.length; j++) {
           finalPrice = available_location[j].price_details.final_price;
       };
});

function razorpay() {      
    $.ajax({
        url: 'http://test4238.wickedride.com//api/cancelledEnquiries/add',
        type: 'GET',
        data: {
        },
        success: function(data){
           alert(finalPrice );
        }
    });
}
0
ProEvilz On

Just define it outside the function and remove var from before final price in available();. Now it can be accessed by both functions.

 var finalPrice = 0;

 function available(id){
    $.ajax({
        method:"GET",           
    }).done(function(data){        

     for(j=0; j<available_location.length; j++) {

       finalPrice =available_location[j].price_details.final_price;
   };
});

function razorpay() {      
    $.ajax({
        url : 'http://test4238.wickedride.com//api/cancelledEnquiries/add',
        type : 'GET',
        data : {

         },
        success: function(data){
              alert(finalPrice );
           }
    });
}
0
Orelsanpls On

If the second call require the first, use of callbacks. If it doesn't call boths, and wait for both answers before you perform the treatment which is to calculate the finalPrice and use it.

It is bad to create a global variable named finalPrice because I'm pretty sure you are running theses both request in parralel, which would result to uncertain behavior if razorpay resolve before available.


First case example

 function available(id, callback) {
  $.ajax({
    method: "GET",
  }).done(function(data) {

    var finalPrice = 0;

    for (j = 0; j < available_location.length; j++) {
      finalPrice = available_location[j].price_details.final_price;
    }

    callback(finalPrice);
  });

  function razorpay(finalPrice) {
    $.ajax({
      url: 'http://test4238.wickedride.com//api/cancelledEnquiries/add',
      type: 'GET',
      data: {

      },
      success: function(data) {
        alert(finalPrice);
      }
    });
  }

  available(id, function(finalPrice) {
    razorpay(finalPrice);
  });