[jQuery] Re: functions after $.get work strange
Erik Beeson wrote: The value from $.get CAN NOT be returned from isTracked because isTracked will have returned before the $.get callback executes. Shame :/ But thank you for that answer. I was fighting because I thought that it is possible. Waste of time :/ So I have no choice and put all code to callback (I suppose that I can do my thing without jquery but too much code for that...) Thank very much Erik, your answers are very clear and helpful (even if I was confused before, but javascript, to be honest, is something new for me :)) Michael Geary wrote: The only way to do that is to use a synchronous $.ajax call: http://docs.jquery.com/Ajax/jQuery.ajax#options But beware! This has the extremely negative effect of completely locking up the browser window - and all other browser windows in many browsers - until your server returns the ajax data. Good to know that there is an option :) but its not good option for me (and almost for everyone I guess). But maybe one day I will need that. Thank you too Michael for answers! Regards Michael
[jQuery] Re: functions after $.get work strange
$.get is asynchronous, meaning the call to $.get returns immediately, even before the callback has happened. To answer your specific question, setting a variable who's scope is outside the callback is as easy as defining the variable outside the callback: var foo; $.get(..., function() { foo = ...; }); But to address the actual problem that you're having, here are the order in which your functions are being executed: Enter isTracked Enter $.get Leave $.get Leave isTracked Enter callback Leave callback So you see, by the time isTracked returns, your callback hasn't executed yet, which is why you see null the first time, and the first value the second time. To do what you want to do, you need to step back a level. Presumably, you do something with the return value from isTracked, so you don't want isTracked to return until it has said value. Say you have: var tracked = isTracked(code); // do something with tracked. To do what you want, you need to pass a callback to isTracked that will get called once the data is available. In your case, you could just pass the callback along to $.get. So your setup might look something like: function isTracked(personcode, callback) { $.get('trackstudent/istracked.php', {'personcode': personcode}, callback); } isTracked(code, function(tracked) { // do something with tracked, exactly as you would have done above. }); This is just part of how JavaScript works, and part of why anonymous functions are so prevalent. Hope it helps. --Erik On 9/27/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Im trying to set somehow variable inside of $.get callback and get outside this callback. Im doing it in this way (because I dont know any other way): function isTracked(personcode) { ret='false'; // it should return string 'true' or 'false' $.get('trackstudent/istracked.php',{'personcode': personcode}, function(data) { //because I dont know how to set variable inside of callback and get outside if ($('span.tempvar').length==0) $('body').append('span class=tempvar style=display: none'+data +'/span'); else $('span.tempvar').html(data); // now I should have at the end of body a new element span alert('1: '+$('span.tempvar').html()); }); //get variable outside $.get callback ret=$('span.tempvar').html(); alert('2: '+ret); //return ret; //ret have proper value } ... when I run this function first time I have two alert messages (data in $.get is 'true'): first: 1: true second: 2: null //second should be: 2: true; when I run second time I have (data in $.get is 'false'): first: 1: false second: 2: true //old value! (dont know why!) //second should be false; When Im checking genereted by javascript source code I see my span How it is possible?! or maybe you have another solution how to set variable inside of $.get callback and get outside TIA Michael
[jQuery] Re: functions after $.get work strange
Erik Beeson wrote: $.get is asynchronous, meaning the call to $.get returns immediately, even before the callback has happened. To answer your specific question, setting a variable who's scope is outside the callback is as easy as defining the variable outside the callback: Erik - your explanation was so clear and concise and packed with other very useful info, it should go on the jQuery wiki as a tutorial IMHO! Guy
[jQuery] Re: functions after $.get work strange
function isTracked(personcode, callback) { $.get('trackstudent/istracked.php', {'personcode': personcode}, callback); } isTracked(code, function(tracked) { // do something with tracked, exactly as you would have done above. }); I thought that I understand that but Im doing something wrong. (istracked.php has 'true' or 'false') --- var r; function isTracked(personcode, callback) { $.get('trackstudent/istracked.php',{'personcode': personcode}, callback); return r; } $(document).ready(function() { alert(isTracked('10591891',function(data) { r=data; })); }); istracked.php returns 'true' result of alert:undefined should be: true Callback is still running last . I have no idea for that :/ TIA Michael
[jQuery] Re: functions after $.get work strange
First of all - Thank You very much - it is good lesson. To answer your specific question, setting a variable who's scope is outside the callback is as easy as defining the variable outside the callback: var foo; $.get(..., function() { foo = ...; }); I tried that before: function test() { var ret; $.get(..., function(data) { ret= data; }); alert(ret); } result: undefined To do what you want, you need to pass a callback to isTracked that will get called once the data is available. In your case, you could just pass the callback along to $.get. So your setup might look something like: function isTracked(personcode, callback) { $.get('trackstudent/istracked.php', {'personcode': personcode}, callback); } isTracked(code, function(tracked) { // do something with tracked, exactly as you would have done above. }); That works perfectly! Now I can do it in my (poor) way, but in other way... (to be honest I dont know how to do that) --- function isTracked(personcode, callback) { var ret; $.get('trackstudent/istracked.php',{'personcode': personcode}, callback); return ret; } ... if (isTracked(personcode, function(data) { ret=data; })=='true') { ... //is tracked } else // isnt tracked - isTracked does not return any value. So how can I get what $.get gets from .php and return from isTracked function? (of course without adding anything to html as I tried before or without using cookies) Thanks Michael
[jQuery] Re: functions after $.get work strange
You're still expecting things to happen in the wrong order. It's *inside the callback* that the data becomes available, and this is long after isTracked() returns. Try this instead: var r; function isTracked(personcode, callback) { $.get('trackstudent/istracked.php',{'personcode': personcode}, callback); } $(document).ready(function() { isTracked('10591891',function(data) { r=data; alert(r); }); }); If that doesn't quite make sense, sprinkle the code with console.log() statements, run it in Firefox with Firebug enabled, and watch the console to see the order of execution. -Mike -Original Message- From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Thursday, September 27, 2007 7:01 AM To: jQuery (English) Subject: [jQuery] Re: functions after $.get work strange function isTracked(personcode, callback) { $.get('trackstudent/istracked.php', {'personcode': personcode}, callback); } isTracked(code, function(tracked) { // do something with tracked, exactly as you would have done above. }); I thought that I understand that but Im doing something wrong. (istracked.php has 'true' or 'false') --- var r; function isTracked(personcode, callback) { $.get('trackstudent/istracked.php',{'personcode': personcode}, callback); return r; } $(document).ready(function() { alert(isTracked('10591891',function(data) { r=data; })); }); istracked.php returns 'true' result of alert:undefined should be: true Callback is still running last . I have no idea for that :/ TIA Michael
[jQuery] Re: functions after $.get work strange
To answer your specific question, setting a variable who's scope is outside the callback is as easy as defining the variable outside the callback: var foo; $.get(..., function() { foo = ...; }); Ahh, I know know where was my mistake. I tried something like that: function a() { var ret; $.get(..., function() { ret = ...; }); } instead of: var ret; function a() { $.get(..., function() { ret = ...; }); } Thanks! Michael
[jQuery] Re: functions after $.get work strange
Michael Geary wrote: You're still expecting things to happen in the wrong order. It's *inside the callback* that the data becomes available, and this is long after isTracked() returns. Try this instead: Yes, but I want have a function that will return what i get from istracked.php to use it like that: function isTracked(...) { $.get('istracked.php',...); return value_from_istrackedphp } //and then if (isTracked(...)=='true') //thing A else //thing B --- var r; function isTracked(personcode, callback) { $.get('trackstudent/istracked.php',{'personcode': personcode}, callback); } $(document).ready(function() { isTracked('10591891',function(data) { r=data; alert(r); }); }); So in that case I dont have to use callback in isTracked. I can do the same thing in this way: function isTracked(personcode) { $.get('trackstudent/istracked.php',{'personcode': personcode}, function(data) { r=data; alert(r); }); } and i still will have alert with proper value, but its not what I want. I want to get data from php file and return it from isTracked function. ( to do something like that: ... if (isTrackes(..)=='true')) Thanks Michael
[jQuery] Re: functions after $.get work strange
Michael Geary wrote: You're still expecting things to happen in the wrong order. It's *inside the callback* that the data becomes available, and this is long after isTracked() returns. From: [EMAIL PROTECTED] Yes, but I want have a function that will return what i get from istracked.php to use it like that: function isTracked(...) { $.get('istracked.php',...); return value_from_istrackedphp } //and then if (isTracked(...)=='true') //thing A else //thing B The only way to do that is to use a synchronous $.ajax call: http://docs.jquery.com/Ajax/jQuery.ajax#options But beware! This has the extremely negative effect of completely locking up the browser window - and all other browser windows in many browsers - until your server returns the ajax data. Do you want to do this this merely for coding convenience, or is there an actual reason that your code *has* to work this way? If it's just for convenience, I strongly recommend using a callback function instead. But if there's a real need for it, the synchronous $.ajax will do the trick. -Mike