it looks like on addLoadEvent
, you are adding a window.onload
handler, this works when you call from createGame
when when it is called the window.onload
is not yet fired but when you are calling it from the success handler the onload event might have already fired because the ajax is processed asynchronously
JQuery .ajax dont call function
-
26-06-2022 - |
Вопрос
I have object "game" and when i call create game, its use jquery ajax... everything works ok, but when i want to call from ajax success function addLoadEvent it doesnt call it, when i try call this function from createGame (commented part of code here) its works... do you know why i cant call it from ajax success? i try console log from success and it was print in console so ajax works well. Thank everybody for help
var game=new ttt_game();
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
function ttt_game () {
this.createGame = createGame;
function createGame(){
/*addLoadEvent(function(){
document.getElementById('player1_n').textContent=player1+':';
document.getElementById('player2_n').textContent=player2+':';
document.getElementById('turn').textContent='Čaká sa na príchod súpera.';
});*/
$.ajax({
type: "POST",
url: "process.php",
data: {'function': 'create','game_id': game_id,'player1': player1},
dataType: "json",
success: function(data){
addLoadEvent(function(){
document.getElementById('player1_n').textContent=player1+':';
document.getElementById('player2_n').textContent=player2+':';
document.getElementById('turn').textContent='Čaká sa na príchod súpera.';
});
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
}
game.createGame();
Решение
Не связан с StackOverflow