Source code of Chess Timer

<?php

//chessTimer.php
//Author: Qiang Gan
//for Chess Club

?>

<html>
<head>
<title>Chess Timer</title>
</head>
<body>
<script type=”text/javascript”>
<!–
var player_1_timer = “600”; //initial value = 10 mins
var player_2_timer = “600”;
var current_timer = “”;
//initial 1 sec trigger

var myVar = setInterval(function(){countDown(current_timer)}, 1000);
//stop watch is using to delay required seconds.
var myStoper = setTimeout(function(){pauseTimer()},1000);

//convert integer into a min:sec
function convertIntoTime(value)
{
var min = Math.floor(value/60);
var sec = value%60;
if( min < 10 )
{
min = “0”+min;
}
if( sec < 10 )
{
sec = “0”+sec;
}
var timer_value = min+”:”+sec;
return timer_value;
}
//count down the current timer
function countDown(timer_name)
{
if(timer_name ==”)
{
return true;
}
else if(timer_name == ‘player_2_time’)
{
player_2_timer –;
current_value = player_2_timer;
}
else if(timer_name == ‘player_1_time’)
{
player_1_timer –;
current_value = player_1_timer;
}
if( current_value <= 0 )
{
current_value = 0; //if it’s 00:00, then stop the counter
}

if( current_value < 60 )
{
//less than 60 second, show red color
document.getElementById(timer_name).innerHTML=”<font color=’RED’>”+convertIntoTime(current_value)+”</font>”;
}
else
{
document.getElementById(timer_name).innerHTML=convertIntoTime(current_value);
}
}
//set current timer
function setTimer(timer_name)
{
current_timer = timer_name;
}
//pause timer
function pauseTimer()
{
current_timer = ”;
}
//allow the user to set timer initial value
function setTime()
{
var settime = prompt(“Set timer:”, “10”);
player_1_timer = settime*60;
player_2_timer = settime*60;
document.getElementById(‘player_1_time’).innerHTML=convertIntoTime(player_1_timer);
document.getElementById(‘player_2_time’).innerHTML=convertIntoTime(player_2_timer);
}
//deal with each click
function oneClick(timer_name)
{
pauseTimer();//pause the timer first
clearTimeout(myStoper);//clear previous stop watch
myStoper = setTimeout(function(){setTimer(timer_name)},2000);//set new stop watch 2 seconds, then start timer
}
//–>
</script>

<form id=”” name=”” method=”post” action=”chessTimer.php”>
<table>
<tr>
<td rowspan=2 ><font face=”verdana” id=”player_1_time” size=”5″>10:00</font></td>
<td><input type=”button” name=”reset” value=”Reset” onclick=”setTime();” /></td>
<td rowspan=2 ><font face=”verdana” id=”player_2_time” size=”5″>10:00</font></td>
</tr>
<tr>
<td><input type=”button” name=”pause” value=”Pause” onclick=”pauseTimer();” /></td>
<tr>
<td><font face=”verdana” size=”10″><input type=”button” name=”white” value=”White” style=”width:75px;height:75px” onclick=”oneClick(‘player_2_time’);”/></font></td>
<td></td>
<td align=”right”><input height=”40″ type=”button” name=”black” value=”Black” style=”width:75px;height:75px” onclick=”oneClick(‘player_1_time’);”/></td>
</tr>
</table>
</form>
</body>
</html>

Advertisements

About qianggan

Sr. Software Engineer
This entry was posted in Computers and Internet. Bookmark the permalink.

One Response to Source code of Chess Timer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s