Обычно, если вы хотите запустить что-то после завершения запроса, вы можете поместить его в success
callback. Так что небольшая модификация вашего кода может делать то, что вы хотите
function values_database(){
$.ajax({
type: "POST",
url: 'events.php',
dataType:"json",
data: {
dal_mese1: 'example'
},
success: function (result) {
var return_php = JSON.parse(JSON.stringify(result));
values.push(return_php); //VALUES FOR ANIMATIONS
$(window).scroll(function(){
var top_window2 = $(window).scrollTop();
var bottom_window2 = top_window2 + $(window).height();
var top_statistiche = $('#somedivs').offset().top;
if(((top_statistiche >= top_window2) && (top_statistiche <= bottom_window2))){
animation_somedivs();
}
});
}
}
function animation_somedivs(){
//use values global array (with inside value from database, if ajax call is finish before "this" function is run
}
Редактировать
В случае, если ваш ajax-запрос выполняется более одного раза за загрузку страницы, вам понадобятся некоторые изменения.
function handle_scroll() {
var top_window2 = $(window).scrollTop();
var bottom_window2 = top_window2 + $(window).height();
var top_statistiche = $('#somedivs').offset().top;
if(((top_statistiche >= top_window2) && (top_statistiche <= bottom_window2))){
animation_somedivs();
}
}
}
function values_database(){
$.ajax({
type: "POST",
url: 'events.php',
dataType:"json",
data: {
dal_mese1: 'example'
},
success: function (result) {
var return_php = JSON.parse(JSON.stringify(result));
values.push(return_php); //VALUES FOR ANIMATIONS
$(window).off('scroll', handle_scroll);
$(window).on('scroll', handle_scroll);
}
}
function animation_somedivs(){
//use values global array (with inside value from database, if ajax call is finish before "this" function is run
}