秒表清除变量javascript

时间:2014-06-24 06:47:12

标签: javascript

嘿,我对使用javascript非常不熟悉...我只需要帮助清除时间(这应该会停止时间然后清除它)在我的脚本中。请参阅:function clear()thanks :)如果您有任何关于将stop \ start按钮合并到一个功能中的建议,也应该受到赞赏。再次感谢你!

<script type='text/javascript'>
var clsStopwatch = function () {

var startAt = 0;
var lapTime = 0;

var now = function () {
    return (new Date()).getTime();
};

this.start = function () {
    startAt = startAt ? startAt : now();
};

this.stop = function () {
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0;
};

this.time = function () {
    return lapTime + (startAt ? now() - startAt : 0);
};
};

var x = new clsStopwatch();
var $time;
var clocktimer;

function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}

function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';

m = Math.floor(time / (60 * 1000));
time = time % (60 * 1000);
s = Math.floor(time / 1000);
ms = time % 1000;

newTime = pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 2);
return newTime;
}

function show() {
$time = document.getElementById('time');
update();
}

function update() {
$time.innerHTML = formatTime(x.time());
}

function start() {
clocktimer = setInterval("update()", 1);
x.start();
}

function stop() {
x.stop();
document.getElementById('counter').value = formatTime(x.time());
clearInterval(clocktimer);
}

function clear() {
x.stop();
????????? this is the function i need help on

}
 </script>

HTML:

<body onload="show();">

<form action="submit.php" method="post"/>

Time: <span id="time"></span><br/>
<!--<input type="text" name-"time">-->
<input type="button" value="Start" onclick="start();">
<input type="button" value="Stop" onclick="stop();">
<input type="button" value="Clear" onclick="clear();">
<input type="submit" value="Save" onclick="stop();">
<br/><br/>

1 个答案:

答案 0 :(得分:1)

您忘记将show()放入start()函数

这是工作小提琴http://jsfiddle.net/64gFm/

clear()功能更改为clearWatch(),因为clear是一个inbult功能
新更新的Js小提琴http://jsfiddle.net/64gFm/1/

function clearWatch() {
    x.stop();
    x.clear();
    clearInterval(clocktimer);
    update();
}

希望,它可能对你有所帮助。祝你今天愉快。 :)