开发者

javascript to replace button text with images...trigger button with external event

I found this stopwatch javascript, complete with "Start", "Stop" and "Reset" buttons. The functionality of this script is great, but I would like to spruce it up a bit. I've managed to style the button background with CSS border-image, but I want to use javascript to replace the "Start", "Stop" and "Reset" text with icons.

I would also like to make the timer start when the user clicks on another area. For example, I may use this to track how long a user takes to solve a puzzle...so it would be great if the "Start" button could be activated upon the user's FIRST event recorded on the puzzle. I though about making an event listener for any click, but I'm afraid that may cause the button to toggle Start/Stop on each event.

Lastly, it would be nice if the "Stop" button triggered a hidden div with a message screen that shows up on top of the puzzle while the game is paused. Below is a snippet of my code

JAVASCRIPT

var sec开发者_如何学C = 0;
var min = 0;
var hour = 0;
function stopwatch(text) {
sec++;
if (sec == 60) {
sec = 0;
 = min + 1; }
else {
min = min; }
if (min == 60) {
min = 0; 
hour += 1; }

if (sec<=9) { sec = "0" + sec; }
document.clock.stwa.value = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" +        min : min) + " : " + sec;

if (text == "Start") { document.clock.theButton.value = "Stop";}
if (text == "Stop") { document.clock.theButton.value = "Start"; }

if (document.clock.theButton.value == "Start") {
window.clearTimeout(SD);
alert("Timer is Paused...Resume?");
return true; }
SD=window.setTimeout("stopwatch();", 1000);
}

function resetIt() {
sec = -1;
min = 0;
hour = 0;
if (document.clock.theButton.value == "Stop") {
document.clock.theButton.value = "Start"; }
window.clearTimeout(SD);
}

CSS

.button {
border-width:0 5px; -webkit-border-image:url(../images/toolButton-791569.png) 0 5 0 5;   padding:7px 0; font: bold 12px Arial,sans-serif; color:#fff; width:auto; margin:5px 10px 0 0; width:auto; height: 25px; text-align:center; cursor:pointer;
}

HTML

   <div id="timer">
   <form name="clock">
   <input type="text" size="14" name="stwa" value="00 : 00 : 00" style="text-align:center; position: relative; top: 0px; background: #ccc; font-weight: bold; font-size: 14px;" />
   <br/><input type="button" class="button" name="theButton" onClick="stopwatch(this.value);" value="Start" />
   <input type="button" class="button" value="Reset" onClick="resetIt();reset();" />
   </form></div>

I'm sure the brilliant programmers in this community might find this question to be trivial, but please forgive me. I am a novice programmer and this challenge is causing me many sleepless nights. Please help...

Thanks, Carlos


Here you go http://jsfiddle.net/mplungjan/C4wjV/

I did not do the pause button

<style type="text/css">
img { height:50px }
#stwa {text-align:center; position: relative; top: 0px; background: #ccc; font-weight: bold; font-size: 14px;}
</style>

<script type="text/javascript">
var buttons = {
    Start:"http://morethanvoice.net/m1/img/play.gif",
    Stop:"http://morethanvoice.net/m1/img/pause.gif"
}
var sec = 0,min = 0,hour = 0,SD;

function stopwatch(text) {
  sec++;
  if (sec === 60) {
    sec = 0;
    min += 1;
  }
  if (min === 60) {
    min = 0;
    hour += 1;
  }

  document.clock.stwa.value = ((hour<10) ? "0"+hour : hour) + " : " +
        ((min<10) ? "0" + min : min) + " : " +
        ((sec<10)? "0" + sec:sec);

  if (text) { // i.e. we are clicked
    if (text==="Stop") window.clearTimeout(SD);
    text = (text==="Start")?"Stop":"Start";
    document.getElementById('stopstart').title=text;
    document.getElementById('stopstarticon').src=buttons[text];  
  }
    if (!text || text === "Stop") SD=window.setTimeout(function() {stopwatch()}, 1000);
  return false;
}

function resetIt() {
  sec = -1;
  min = 0;
  hour = 0;
  document.getElementById('stopstart').title = "Start"
  document.getElementById('stopstarticon').src=buttons["Start"];
  window.clearTimeout(SD);
  document.clock.stwa.value=document.clock.stwa.defaultValue;
  return false;
}
</script>

    <form name="clock"><input type="text" size="14" id="stwa" name="stwa" value="00 : 00 : 00"  />
</form><br/>

<a href="#" id="stopstart" title="Start"
onClick="return stopwatch(this.title)"><img id="stopstarticon" src="http://morethanvoice.net/m1/img/play.gif" border="0" /></a>
<a href="#" onClick="return resetIt()"><img src="http://morethanvoice.net/m1/img/rewind.gif" border="0" /></a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜