0

my javascript function was working perfectly and i tested it hundreds of times then suddenly start to giving me jQuery.Deferred exception: $ is not defined

my function :

function UserColor() { // Is used to check which color the opponent has
        var MoveString = BoardToFen();
         $.ajax({
            type:'POST',
            url:'/getcolor',
            data:{MoveString:MoveString},
            dataType:'json',
            cache: false,
            success:function(data){
                //i can getting the output of the data.msg when i using console.log(data.msg) but it still saying jQuery.Deferred exception: $usercolor is not defined 
                if (data.msg == "white"){
                    $usercolor=0;

                } else{


                    $usercolor=1;
                }
            }
        });
return $usercolor;
}

the error massage :

jQuery.Deferred exception: $usercolor is not defined

the line where the error came from return $usercolor;

my html scripts links

            <head>

                <META HTTP-EQUIV=Refresh; 
                <meta charset="utf-8">

                <script src="/socket.io/socket.io.js"></script>

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

                <link rel="stylesheet" href="/cssFiles/styles.css"> 
                <title>JSChess</title>
                <link href="/cssFiles/styles.css" rel="stylesheet" type="text/css">
                <link rel="stylesheet" href="/cssFiles/js/bootstrap-3.3.7/dist/css/bootstrap.css">

                <script type="text/javascript"> if (!window.console) console = {log: function() {}}; </script>

             </head>
             <body onbeforeunload="return myFunction()" style="background-color: black;color: white;">


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

                <script src="/cssFiles/js/defs.js"></script>
                <script src="/cssFiles/js/io.js"></script>
                <script src="/cssFiles/js/board.js"></script>
                <script src="/cssFiles/js/movegen.js"></script>
                <script src="/cssFiles/js/makemove.js"></script>
                <script src="/cssFiles/js/perft.js"></script>
                <script src="/cssFiles/js/evaluate.js"></script>
                <script src="/cssFiles/js/pvtable.js"></script>
                <script src="/cssFiles/js/search.js"></script>
                <script src="/cssFiles/js/protocol.js"></script>       
                <script src="/cssFiles/js/guiMultiPlayer.js"></script>
                <script src="/cssFiles/js/bootstrap-3.3.7/dist/js/bootstrap.js"></script>
                <script src="/cssFiles/js/main.js"></script>
                <script src="/cssFiles/js/deleteDB.js"></script>

update: after adding my return $usercolor to success

function UserColor() { // Is used to check which color the opponent has
        var MoveString = BoardToFen();
         $.ajax({
            type:'POST',
            url:'/getcolor',
            data:{MoveString:MoveString},
            dataType:'json',
            cache: false,
            success:function(data){

                if (data.msg == "white"){


                    $usercolor=0;
                } else{

                    $usercolor=1;
                }
                return $usercolor;
            }
        });
  //return $usercolor;
}

now i getting undefined when i use console.log(UserColor());

shar
  • 1,233
  • 6
  • 20
  • 30
  • `$usercolor` is not valid JQuery syntax. Where did you define the instance of that variable? What is your goal with that code? Do you want the value in an element or assigned to a variable? – daddygames Sep 05 '19 at 18:48
  • Move "return $usercolor;" inside the success block. It is not defined or assigned outside this block. – Nawed Khan Sep 05 '19 at 18:58
  • initialize usercolor right after your initialize MoveString. var usercolor; then remove the $ from your $usercolor so it is just usercolor –  Sep 05 '19 at 19:00
  • @NawedKhan now i getting undefined output when i using console.log(UserColor()) – shar Sep 05 '19 at 19:23
  • @Mike now i getting undefined on my console.log(UserColor()) – shar Sep 05 '19 at 19:23
  • Where are you calling console.log(UserColor()) ?? Please update the code where you have moved the "return $usercolor;" inside the success block – Nawed Khan Sep 05 '19 at 19:28
  • i updated my post @NawedKhan – shar Sep 05 '19 at 19:29
  • Just add the variable outside the function globally or just inside the function to initialize it. var $usercolor = 0; – Keith Sep 05 '19 at 19:40
  • @Keith if i maked it globally any one will be able to editing it using the browser console right? – shar Sep 05 '19 at 19:44
  • Anyone can create a global variable and then use it in a function anyways, so it doesn't make a difference. All I'm saying is you are using a variable currently that you haven't initialized yet so it can't find it. – Keith Sep 05 '19 at 19:48
  • As Keith suggested, add "var $usercolor = 0;" after "var MoveString = BoardToFen();" – Nawed Khan Sep 05 '19 at 19:53

0 Answers0