I'm trying to cerate simple chess code, that I will understand how it works. I did find one or two samples, but I don't undersstand that code. (link 1, link 2) Can you help me?
I try to combine countdown timer to a checc clock, but with no succes. When I press button "start" the clock will start count down for player 1 (white). When this player press a button ("White") the time will stop for this player and start count down for black player and so when this one press button "black" the timer start count down for white. Do I need 2 seperate timers or it will be enough one that can 'save' timers? How do I set the "add time"? This is when player press button, the timer add few seconds?
this is what I try so far:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Chess clock</title>
<style type="text/css">
#MoveWhite{
margin-left: 50px;
float:Left;
width:200px;
height: 100px;
background-color: silver;
text-align: center;
}
#MoveBlack{
float:left;
width:200px;
height: 100px;
background-color: blue;
text-align: center;
}
#Begin{
margin-left: 50px;
float:left;
width: 400px;
height: 100px;
background-color: green;
text-align: center;
}
#SetUp{
margin-left: 50px;
margin-right: 50px;
float: left;
width: 400px;
height: 100px;
background-color: green;
}
body{
width: 500px;
background-color: #06F;
}
</style>
<script type="text/javascript">
var minutesleft = 0;
var secondsleft = 60;
var finishedtext = "Lost"
ura = new Date();
ura.setMinutes(ura.getMinutes() + minutesleft);
ura.setSeconds(ura.getSeconds() + secondsleft);
function cdWhite(){
now = new Date();
diff = ura - now;
diff = new Date(diff);
var sec = diff.getSeconds();
var min = diff.getMinutes();
if (min < 10){
min = "0" + min;
}
if (sec < 10){
sec = "0" + sec;
}
if(now >= ura){
clearTimeout(timerID);
document.getElementById("cdtime").innerHTML = finishedtext;
}
else{
document.getElementById("cdtime").innerHTML = min + ":" + sec;
}
timerID = setTimeout("cdWhite()", 10);
}
</script>
</head>
<body>
<div id = "Begin">
<p>White player start game when press "Start game" button</p>
<button onClick="cdWhite()">Start game!</button>
</div>
<div id = "num1">
<div id = "MoveWhite">
<h1 id = "White"><span id="cdtime">TimerWhite??<!-- Lost (if white player loose) -->
</span></h1>
<button onClick="startBlack()">White</button>
</div>
<div id = "MoveBlack">
<h1 id = "Black">TimerBlack??</h1> <!-- Lost (if black player loose) -->
<button onClick="startWhite()">Black</button>
</div>
</div>
<div id = "SetUp">
<p>Set different starting time <br/>
Set starting time: ___min ___sec</p>
<p>Add time: ____ sec</p>
</div>
<ul id = "cas">
</ul>
</body>
</html>