basically i want to hold a parameter that retrieve value from $.post() call like this:
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 reach at last post (last page) i should check with lastpage variable that has a value returned from getLastPage() function.
I’m pretty blur with javascript pointer and all. Please help guys.
update (20/4/2010):
I’ve done the other way around, like this:
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 store the value in hidden input tag (lastpage) and then grab the value again to check it whenever i click forward button.
if you all have more appropriate way please tell me.
You should change your code around like this:
The problem with your overall approach is that with AJAX, you’re dealing with an asynchronous operation. This means that the
function(data) { }portion doesn’t run then, it runs later, so yourreturndoesn’t actually return anything, it’ll beundefined.Instead of this approach, you need to call
$.post()then call whatever function relies on this data to continue as part of$.post()‘s callback. After doing that your code order looks like this:$.post()executes, firing off a request to the server$.post()runs