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());