Saturday, 20 May 2017

ajax getting the new data but not alwayes updating on the webpage

i working on a code to getting data from database for object position (sprite image ) and ajax should updating the position for object every 2 seconds and set pieces on the right position like the position on database but that's happend some time and sometime needs to refresh the page many times to get that so where's the problem I'm using node.js and javascript and html and jquery and json encode I don't get any error because any of them but I get the write position when I use console.log to check it in the client side or in server so are the error on the html or what ?

<!DOCTYPE html>
<html>

                <link rel="stylesheet" href="http://localhost:5000/cssFiles/styles.css"> 
                <title>JSChess</title>
                <link href="http://localhost:5000/cssFiles/styles.css" rel="stylesheet" type="text/css">
                <link rel="stylesheet" type="text/css" href="http://localhost:5000/cssFiles/styles.css" />

<body>


                <h2>Welcome <span style="color:green"><h1>your nam</h1>! </span>You are playing against <span style="color:red"><h1>your nam</h1>! </span>
                </h2>   
                <div id="FenInDiv" style="display:none;">           
                    <input type="text" id="fenIn"/>     
                    <button type="button" id="SetFen">Set Position</button> 
                </div>  
                <div id="ThinkingImageDiv">     
                </div>
                <div id="Board">
                </div>
                <div id="CurrentFenDiv" >
                    <span id="currentFenSpan" style="display:none;"></span>     
                </div>              
                <div id="ChatMessages">
                </div>
                <div id="AvailablePlayers"></div>

                <div id="ChatMessages"></div>
                <div id="ChatBig"> 
                    <span style="color:green">Chat</span><br/>
                    <textarea id="ChatText" name="ChatText"></textarea>
                </div>
                <button type="button" id="NewGameButton">New Game</button><br/>
                <span id="GameStatus"></span>
        <!--This div not outputted but needed to work  -->          
                <div id="EngineOutput"><br/>
                    <select id="ThinkTimeChoice" style="display:none;">
                    <option value="1">1s</option>
                    <option value="2">2s</option>
                    <option value="4">4s</option>
                    <option value="6">6s</option>
                    <option value="8">8s</option>
                    <option value="10">10s</option>
                    </select><br/><br/><br/>

                    <span id="BestOut" style="display:none;">BestMove:</span><br/>
                    <span id="DepthOut"style="display:none;">Depth:</span><br/>
                    <span id="ScoreOut"style="display:none;">Score:</span><br/>
                    <span id="NodesOut"style="display:none;">Nodes:</span><br/>
                    <span id="OrderingOut"style="display:none;">Ordering:</span><br/>
                    <span id="TimeOut"style="display:none;">Time:</span><br/><br/>
                    <button type="button" id="SearchButton"style="display:none;">Move Now</button><br/>

                    <button type="button" id="FlipButton"style="display:none;">Flip Board</button><br/><br/>
                    <button type="button" id="TakeButton"style="display:none;">Take Back</button><br/><br/><br/>

                </div>


                <script src="js/jquery-3.2.1.min.js"></script>  

                <script src="js/defs.js"></script>
                <script src="js/io.js"></script>
                <script src="js/board.js"></script>
                <script src="js/movegen.js"></script>
                <script src="js/makemove.js"></script>
                <script src="js/perft.js"></script>
                <script src="js/evaluate.js"></script>
                <script src="js/pvtable.js"></script>
                <script src="js/search.js"></script>
                <script src="js/protocol.js"></script>       
                <script src="js/guiMultiPlayer.js"></script>
                <script src="js/main.js"></script>
                <script src="js/deleteDB.js"></script>
               <script src="/socket.io/socket.io.js"></script>


</body>
</html>

the javascript ajax part

function loadLMove(){ 


        $.ajax({
            type:'POST',
            url:'/dispalymove',
            data:{MoveString:MoveString},
            dataType:'json',
            cache: false,
            //timeout: 20000,
            success:function(data){
                if (data.msg != ""){
                    if (MoveString!=data.msg){
                        var now = new Date().getTime();
                        //alert(data.msg);
                        ParseFen(data.msg);
                        ++timesRun;
                         console.log('Move displayed: ' + data.msg + 'Action ' + timesRun + ' started ' + (now - startTime) + 'ms after script start');
                        //PrintBoard();     
                        SetInitialBoardPieces();    
                        GameController.PlayerSide = brd_side;   
                        CheckAndSet();  

                        EvalPosition(); 
                        //PerftTest(5);
                        //newGameAjax();
                        MoveString=data.msg;
                        }
                } else{
                    if (MoveString!=data.msg){
                        ParseFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
                        //PrintBoard();     
                        SetInitialBoardPieces();    
                        GameController.PlayerSide = brd_side;   
                        CheckAndSet();  

                        EvalPosition(); 
                        //PerftTest(5);
                        //newGameAjax();
                    }
                }
            }
        });

}
//---------------------------------------------------------
loadLMove();
setInterval(loadLMove,2000);

my node.js code

app.post('/dispalymove', function (req, res, next) {
var lMove="";
if(req.body.MoveString !== null){

     Move.setMoveUserId(req.user.id);
     Move.setMoveString(req.body.MoveString);
     a.getLastMove(req.user.GameId,function(move){
      console.log("Return from display req.body:",req.body.MoveString);
      console.log("Return from display themove:",move);
        res.json({"msg": move, "loggedin": "true"});

      });


    } else {

              var output = {"msg": lMove, "loggedin": "true"}; // <=== here  always equals ""

               res.json(output);

    }


});

getlastmove function code

getLastMove(id,callback){


        var MoveRequest = "SELECT * FROM users ORDER BY id";    
        var query = connection.query(MoveRequest, function(err,rows, result) {
        if (rows.length == 0) { 
            return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
        }
        if (rows.length > 0) {
            for (var i in rows) {

                var move = rows[i].MoveString; 
                if (rows[i].GameId == id){

                    callback(move);
                    return;
                }

            }
        }

        });


    }



via dark night

No comments:

Post a Comment