Trying to do what should be a simple task and cannot find a solution. I am trying to set a variable named outside of a $.get request but it does appear to be setting.
Here is my code:
$(document).ready(function() {
var totalPages
var currentPage = 1;
//this $.get returns a number
$.get('getTotalPages.php', function(data) {
totalPages = data;
});
//just an example of applying the result to something else
if (currentPage < totalPages) {
... do something...
}
});
Any ideas?
I think you’re looking for this:
$.get() is asynchronous, but it’s only shorthand for full $.ajax() function, which you can set to be synchronous and easily retrieve responseText property.
Note that synchronous request forces the script to wait on response, so other actions will be disabled while the request is active.