Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript秒表限制最长时间_Javascript - Fatal编程技术网

javascript秒表限制最长时间

javascript秒表限制最长时间,javascript,Javascript,我有一个javascript秒表,以MM:SS(分:秒)为单位显示时间。如果时间达到59:59 MM:SS,则应自动停止(直到用户点击清除并再次启动),而不是增加到60:00、60:01等。如何设置最长时间以防止出现这种情况?下面的代码允许用户停止、启动、清除,并保存秒表,这是完美的减去限制javascript上允许的最大时间。如有任何帮助或文件链接,将不胜感激 javascript: <script type='text/javascript'> var clsStopwatch

我有一个javascript秒表,以MM:SS(分:秒)为单位显示时间。如果时间达到59:59 MM:SS,则应自动停止(直到用户点击清除并再次启动),而不是增加到60:00、60:01等。如何设置最长时间以防止出现这种情况?下面的代码允许用户停止、启动、清除,并保存秒表,这是完美的减去限制javascript上允许的最大时间。如有任何帮助或文件链接,将不胜感激

javascript:

<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.clear = function () {
lapTime = 0;
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 = 0;
var newTime = '';

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

newTime = pad(m, 2) + ':' + pad(s, 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 save() {
x.stop();
document.getElementById('counter').value = formatTime(x.time());
clearInterval(clocktimer);           
}

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

</script>
<form action="save.php" method="POST"/>
<h1>Time: <span id="time"></span> <br/></h1> 
<!--<input type="text" name-"time">-->
<input type="hidden" value="" id="counter" name="counter">
<input type="button" value="Start" onclick="start();">
<input type="button" value="Stop" onclick="stop();">
<input type="button" value="Clear" onclick="clearWatch();">
<input type="submit" value="Save" onclick="save();">

var clsStopwatch=函数(){
var startAt=0;
var lapTime=0;
var now=函数(){
return(新日期()).getTime();
};
this.start=函数(){
startAt=startAt?startAt:now();
};
this.stop=函数(){
lapTime=startAt?lapTime+now()-startAt:lapTime;
startAt=0;
};
this.clear=函数(){
lapTime=0;
startAt=0;
};
this.time=函数(){
返回lapTime+(startAt?now()-startAt:0);
};
};
var x=新的clsStopwatch();
var$时间;
无功时钟;
功能板(数量、大小){
var s=“0000”+num;
返回s.substr(s.length-size);
}
函数格式化时间(time){
var h=m=s=0;
var newTime='';
m=数学楼层(时间/(60*1000));
时间=时间%(60*1000);
s=数学楼层(时间/1000);
newTime=pad(m,2)+':'+pad(s,2);
返回新时间;
}
函数show(){
$time=document.getElementById('time');
更新();
}
函数更新(){
$time.innerHTML=formatTime(x.time());
}
函数start(){
时钟计时器=设置间隔(“更新()”,1);
x、 start();
}
函数停止(){
x、 停止();
document.getElementById('counter')。value=formatTime(x.time());
clearInterval(时钟计时器);
}
函数save(){
x、 停止();
document.getElementById('counter')。value=formatTime(x.time());
clearInterval(时钟计时器);
}
函数clearWatch(){
x、 停止();
x、 清除();
clearInterval(时钟计时器);
更新();
}
html/php:

<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.clear = function () {
lapTime = 0;
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 = 0;
var newTime = '';

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

newTime = pad(m, 2) + ':' + pad(s, 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 save() {
x.stop();
document.getElementById('counter').value = formatTime(x.time());
clearInterval(clocktimer);           
}

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

</script>
<form action="save.php" method="POST"/>
<h1>Time: <span id="time"></span> <br/></h1> 
<!--<input type="text" name-"time">-->
<input type="hidden" value="" id="counter" name="counter">
<input type="button" value="Start" onclick="start();">
<input type="button" value="Stop" onclick="stop();">
<input type="button" value="Clear" onclick="clearWatch();">
<input type="submit" value="Save" onclick="save();">

时间:

更改时间函数,以便如果时间大于最大值,则返回最大值

this.time = function () {
    var duration = lapTime + (startAt ? now() - startAt : 0);
    if (duration > max) {
        duration = max;
    }
    return duration ;
};
将max替换为表示最大持续时间的数值


对于59:59,最大值为(1000*60*59)+(59*1000)。这是因为返回的时间表示是毫秒值,因此59分加59秒

您没有问任何问题。“我有一个javascript秒表,以MM:SS(分:秒)显示时间。如果时间达到59:59 MM:SS,则它应该自动停止(直到用户点击清除并重新启动)而不是增加到60:00、60:01等。“我的目标是指定最长时间,这不是一个问题。这在主题中也应该很清楚。这不是一个问题,因为这不是一个问题,对不起。“这一点在主题中也应该很清楚”——我们不知道你为什么现在陷入困境。这只是
如果(时间经过>60分钟){…}
逻辑。我更新了我的问题。感谢您的输入。“我如何设置最长时间来防止这种情况?”——您只需检查每个勾号经过的时间。所以除了输出多少通过你需要检查它。谢谢我会给这个一个机会。我不打算再在这里发表评论了,因为它太毒了。我想他们只是想指出,它只是一个if语句,在正确的位置,在大多数语言中都是相同的,只是语法上有一些细微的差异。很抱歉打扰你。正如上面所示,我使用this.time=函数()并设置duration>5和duration=5只是为了测试它(我假设这意味着5秒),但是当我单击“开始”时计数器无法启动。这意味着5毫秒。。查看我的更新。5秒等于5000秒