简体   繁体   English

jQuery .ajax不要调用函数

[英]JQuery .ajax dont call function

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? 我有对象“游戏”,当我调用创建游戏时,它使用jquery ajax ...一切正常,但是当我想从ajax成功函数addLoadEvent调用时,它不会调用它,当我尝试从createGame调用此函数时(注释代码的一部分),其工作原理...您知道为什么我无法从Ajax成功中调用它吗? i try console log from success and it was print in console so ajax works well. 我尝试从成功的控制台日志,它是在控制台中打印的,所以ajax效果很好。 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();

它看起来像addLoadEvent ,您要添加window.onload处理程序,这一工程时,从调用createGame时,当它被称为window.onload还没有解雇,但是当你从成功调用它处理程序中的onload事件可能有已被触发,因为ajax是异步处理的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM