The best thing to do is use a callback:
function readMemory(ptr, size, callback)
{
$.post("readMemory.php", { ptr : ptr, size : size}, function(data)
{
callback(data, ptr, size);
});
}
(Note that we have ptr
and size
being passed to callback
as well as data
; this is usually good practice.)
So code you were expecting to use readMemory
like this:
var data = readMemory(ptr, 100);
doSomethingWith(data);
doSomethingElseWith(data);
...will instead look like this:
readMemory(ptr, 100, function(data) {
doSomethingWith(data);
doSomethingElseWith(data);
});
This is because your readMemory
function only starts the POST
operation; it returns before the operation completes (the operation is asynchronous).