-1

How to update javascript variable after ajax call back

var x =1;

$(document).on('click','.classname',function(){
console.log(x);
});

$.ajax({
        type:'post',
        url:tmplUri+'/ajax/test.php',
        cache:false,
        async:false,
        data:{journey:'yes'},
        success: function(data)
        {
          x = data;
         }
});

after ajax call i need to update the variable on that page

Please give any suggestion for this issue

Deeban Babu
  • 729
  • 1
  • 15
  • 49

1 Answers1

2

for this you need a globle variable, just Call a another function to reset you variable after ajax call

  var x // make it globle


    $(document).on('click','.classname',function(){
    console.log(x);
    });

    function resetx(){
     x =1;
    }


    $.ajax({
            type:'post',
            url:tmplUri+'/ajax/test.php',
            cache:false,
            async:false,
            data:{journey:'yes'},
            success: function(data)
            {
              x = data;
             }
    });


calling hierarchy

ajaxcall 
resetx

than check you variable it will give you 1

here the fiddle

Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48