I am attempting to create a spinner control on my webpage, and I have tried the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple spinner</title>
<script type="text/javascript">
function RotateSpinner(spinnerId, up) {
var ele=document.getElementById(spinnerId);
ele.value=parseInt(ele.value)+up;
var t=setTimeout(function(){
if(window.rotate_start)
RotateSpinner(spinnerId,up);
else
clearTimeout(t);
},500);
}
</script>
<style>
*
{
padding: 0;
margin: 0;
}
.spinner
{
list-style: none;
display: inline-block;
line-height: 0;
vertical-align: middle;
}
.spinner input
{
font-size: .45em;
border-width: .5px;
height: 1.5em;
width: 2em;
}
</style>
</head>
<body>
<input id="spinner" type="text" value="0" />
<ul class="spinner">
<li>
<input type="button" value="▲" onmousedown="window.rotate_start=true;RotateSpinner('spinner', 1)" onmouseup="window.rotate_start=false;"/>
</li>
<li>
<input type="button" value="▼" onmousedown="window.rotate_start=true;RotateSpinner('spinner', -1)" onmouseup="window.rotate_start=false;"/>
</li>
</ul>
</body>
</html>
However, the spinner does not behave as expected. When I click the "up" button, I want the value to increment only once, but sometimes it adds the value multiple times.
Could someone please review this and make the necessary fixes?
For a live example, visit this link