Pass from the $ .post () callback to an external variable

Basically I want to save a parameter that retrieves a value from a call to $ .post () as follows:

init = function(){          
var lastpage = getLastPage();
}

        function getLastPage(){
            $.post("getInfo.php",{ 
                last: "yes"
            },
            function(data){
                setLast(data.last);
            },'json');

            return function setLast(data){
                return data;
            }
        }

so when I reach the last message (last page), I have to check with the lastpage variable, which has the value returned by the getLastPage () function.

I am pretty blurring javascript pointer and that’s it. Help the guys.

update (04/20/2010):

I did it the other way around:

init = function(){            
  getLastPage();
  if((page+1) == $("#lastpage").val()){
     alert("this is last post");
  }else{
     page++;
     //get info and display to the page here
  }
}
     function getLastPage(){
        $.post("getInfo.php",{ 
            last: "yes"
        },
        function(data){
            $("#lastpage").val(data.last);
        },'json');
    }

first run the function to temporarily save the value in the hidden input tag (lastpage), and then take that value again to check it when I press the forward button.

If you have a more suitable way, tell me.

+3
source share
1

:

$.post("getInfo.php",{ last: "yes" },
  function(data){ 
    functionToRunAfterYouHaveDataSometimeLater(data.last); 
  }
,'json');

, AJAX . , function(data) { } , , return , undefined.

$.post(), , , $.post(). :

  • $.post() ,
  • $.post()
  • , ,
  • , .
+3

Source: https://habr.com/ru/post/1741800/


All Articles