views:

80

answers:

2

I'm using jquery's $.get() to find out how many entries are in a table. I want the returned value to be an Int but it seems to be something else. How do I work with this? this is the code I have. I'm sure I'm just going about this wrong. My background is many java.

var num = checksize();

function checksize(){
 $.get("../php/checktablezie.php", function(data){
  return data;
 });
}
+1  A: 

Hi ,

Can we write functions like that for ansycronous ajax calls , as it may complete the call in parallel.

gov
Are you asking a question? I think you might be right though. Cause if I put an alert after num=checksize() and another just before return data the one below num=checksize() goes first.
creocare
+2  A: 

You cannot do it like that. $.get is an asynchronous request, so your checksize() function will return instantly. Your anonymous function will be executed when the Ajax call is done, but that will be after checksize() has been returned.

Instead, you'll have to put whatever should be done with num inside your anonymous function, like this:

function checksize(){
 $.get("../php/checktablezie.php", function(data){
  num = data;
  //The code that is in need of num should be put in here.
  //e.g., if you're updating the GUI with the value, put that code here.
 });
}
Emil Vikström