php - Why this is variable does not work? Jquery -


this question has answer here:

i writing following code below, takes data current date php file handle in jquery. until here well. i'm not understand why can not have new value of total variable after picks value coming php file.

day.each(function () {          var $this = $(this);         var the_index = $this.index();         var the_date = $this.find('h3.date').html().substr(0,2);         var the_day = $this.find('h3.day');          /*this variable*/            var total = 0;          $.get('trd/datetime.php', function (date) {              if(that.hasclass('list-'+date.day)) {                 weeklist.find('.item.list-'+date.day).addclass('active');             }              total = date.firstdate;          }, 'json');          console.log(total);       }); 

i don't know if english helps, but, please, tell me wrong i'm doing!

thanks.

the .get call asynchronous -- stuff inside runs after console.log statement.

you want use callback, invoke inside .get handler:

$.get('trd/datetime.php', function (date) {     // ...      callback(total); }, 'json');  function callback(total) {     console.log(total); } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -