I got a function - lets define it as function getdata(after){}
that takes as a after
parameter a function. Quite natural. This function is being run from some other function that has some variables defined in its scope:
$('.button').click(function(){
var rel = $(this).attr('rel');
var mooo = $(this).parent().offset().left;
getdata(function(){alert(rel);console.log(moo)});
});
How do I pass those variables right way, so it would really be the same thing as it was when clicking the button? It cannot be global variable, because user can spam-click button and it should keep the variables as they exactly were during clicking, even when after
function would be called 10 minutes later in kind of random order, because as name says - it is being called AFTER ajax request.
This is only an example - the idea is that this after
function would be variable too, called from totally different places.
Ok, got -1
, because it is unclear what I am asking for, so I try to explain the problem. I don't really understand when variable is passed as variable and when it is passed as pointer of variable that may change before it will get used by function it was passed to.
When I define variable in one function and pass it to another, is it a pointer or a stand-alone variable? When I will call origin function again, and those variables will change - will it affect the function that was "sheduled" before? Or a word var before variable means - define it as totally fresh variable, and every time the function with var is being called, it allocates this variable in memory as totally new one, undependend of anything that happens?
I am generally a PHP, C and Delphi progammer and it is a bit hard for me to understand how those variable scopes work, especially when everything is being async and callbacked.