Prevent race condition with player clicking before AJAX query finishes
authorjudge <[email protected]>
Thu, 20 Jun 2013 15:45:58 +0000 (23:45 +0800)
committerjudge <[email protected]>
Thu, 20 Jun 2013 15:45:58 +0000 (23:45 +0800)
web/qwebchess/js.js

index db91e14..53e9288 100644 (file)
@@ -87,7 +87,10 @@ $(document).ready(function()
                }\r
                \r
                if (legal)\r
+               {\r
+                       canClick = false;\r
                        $.ajax({url : "/cgi-bin/qchess.cgi", data : {x : id[0], y : id[1]}}).done(function(data) {$(this).update(data)});\r
+               }\r
        });\r
 \r
        $.fn.showMoves = function()\r
@@ -213,7 +216,7 @@ $(document).ready(function()
                $(this).html(emptyHTML);\r
 \r
                // Collapse into quantum state if on a black square\r
-               if ((dest.getX() + dest.getY()) % 2 != 0 && (dest.html()[0] == '?' || dest.html()[dest.html().length-1] == '?'))\r
+               if ((dest.getX() + dest.getY()) % 2 != 0 && (dest.html()[0] == '?' || dest.html()[dest.html().length-1] != dest.html()[0]))\r
                {\r
                        oldHTML = dest.html();\r
                        dest.html(oldHTML.replace(/<bold>.*<\/bold>/i, "<bold>?</bold>"));\r
@@ -225,6 +228,7 @@ $(document).ready(function()
        {\r
                console.log("AJAX Response:\n"+data);\r
                var lines = data.split("\n");\r
+               var timeout = false;\r
                for (var i = 0; i < lines.length; ++i)\r
                {\r
                        var tokens = lines[i].split(" ");\r
@@ -237,7 +241,7 @@ $(document).ready(function()
                                        if (s1.html()[4] != '0')\r
                                        {\r
                                                s2 = $("#board").find("#"+tokens[3]+tokens[4]);\r
-                                               canClick = false;\r
+                                               timeout = true;\r
                                                setTimeout((function(x) \r
                                                {\r
                                                        return function() \r
@@ -296,6 +300,8 @@ $(document).ready(function()
                                        break;\r
                        }\r
                }\r
+               if (timeout == false)\r
+                       canClick = true;\r
        }\r
 \r
        //Reset the colour of a square\r

UCC git Repository :: git.ucc.asn.au