下面我有一个javascript倒计时器,工作正常:
<h1><span id='countdown'><?php echo $dbSessionDuration; ?></span></h1>
...
$(document).ready(function() {
var time = <?php echo json_encode($dbSessionDuration); ?>,
parts = time.split(':'),
hours = +parts[0],
minutes = +parts[1],
seconds = +parts[2],
span = $('#countdown');
function correctNum(num) {
return (num<10)? ("0"+num):num;
}
var timer = setInterval(function(){
seconds--;
if(seconds == -1) {
seconds = 59;
minutes--;
if(minutes == -1) {
minutes = 59;
hours--;
if(hours==-1) {
alert("timer finished");
clearInterval(timer);
return;
}
}
}
span.text(correctNum(hours) + ":" + correctNum(minutes) + ":" + correctNum(seconds));
}, 1000);
});
现在我想做的是使用上面的示例并操纵它现在执行相反的操作并创建一个从00:00:00开始的计时计时器(小时,分钟,秒)。
但问题是它不起作用。什么都没发生。我不知道我在下面的功能中是否需要一个小时,因为作为一个计数,小时数可以达到它想要的数小时。但我的问题是如何计算计时器以使其完全正常工作?
<p><input type='text' class='responseTime' name='responsetime' value='00:00:00' /></p>
...
$(document).ready(function() {
var response = "00:00:00",
parts = time.split(':'),
hours = +parts[0],
minutes = +parts[1],
seconds = +parts[2],
input = $('.responseTime');
function correctResponse(responsenum) {
return (responsenum<10)? ("0"+responsenum):responsenum;
}
var responsetimer = setInterval(function(){
seconds--;
if(seconds == +59) {
seconds = 00;
minutes--;
if(minutes == +59) {
minutes = 00;
hours--;
return;
}
}
input.text(correctResponse(hours) + ":" + correctResponse(minutes) + ":" + correctResponse(seconds));
}, 1000);
});
更新
另外作为旁注,我意识到倒数计时器我相信每秒倒计时2秒。如何对倒计时器进行排序并包含在倒计时器中?
下面的代码我已经改为包含增量++并包含.val而不是.text但发生的事情是它从00:00:00开始然后它下降到00:59:60并且然后停下来:
var response = "00:00:00",
parts = time.split(':'),
hours = +parts[0],
minutes = +parts[1],
seconds = +parts[2],
input = $('.responseTime');
function correctResponse(responsenum) {
return (responsenum<10)? ("0"+responsenum):responsenum;
}
var responsetimer = setInterval(function(){
seconds++;
if(seconds == +59) {
seconds = 00;
minutes++;
if(minutes == +59) {
minutes = 00;
hours++;
return;
}
}
input.val(correctResponse(hours) + ":" + correctResponse(minutes) + ":" + correctResponse(seconds));
}, 1000);
});
答案 0 :(得分:2)
在jQuery中,使用val('value')
函数来更改/设置输入字段的值。
将input.text(...)
更改为input.val(...)
另外:
seconds = seconds % 60;
将其带回0
。data-attributes
将值传递给javascript。答案 1 :(得分:2)
这就是我实现这样一个计时器的方式:
function startTimer() {
var time = 0
setInterval(function(){
time++
var sec = time % 60
var min = (time-sec)/60 % 60
var hour = (time-sec-min*60)/3600
var str= hour+':'+("0"+min).slice(-2)+':'+("0"+sec).slice(-2)
$('.responseTime').val(str)
},1000);
}
使用类responseTime
的输入来显示计时器。 (为什么不使用id btw?)
在适当的时候开始调用startTimer()
(在您的示例中使用document.onload=startTimer
)
如果您想从0以外的其他时间开始,请将time
设置为适当的秒数。
对于倒计时,请将time++
替换为time--
(可能会为time==0
添加一些后勤工作)
答案 2 :(得分:1)
您可以轻松修改当前倒数计时器,例如:http://jsfiddle.net/Sergiu/CDpeJ/3
var timer = setInterval(function(){
seconds++;
if(seconds == 60) {
seconds = 00;
minutes++;
if(minutes == 60) {
minutes = 00;
hours++;
}
}
var newTime = hours + ":" + minutes + ":" + seconds;
if(!newTime.match(/^(20|21|22|23|[01]\d|\d)(([:.][0-5]\d){1,2})$/)) newTime = "finished";
$('#timeInput').val(newTime);
}, 1000);
答案 3 :(得分:0)
试试这个
var millis = <?php echo $dbSessionDurationTime; ?> ( diff in seconds )
function displaytimer(){
var hours = Math.floor(millis / 36e5),
mins = Math.floor((millis % 36e5) / 6e4),
secs = Math.floor((millis % 6e4) / 1000);
//Here, the DOM that the timer will appear using jQuery
$('.count').html(hours+':'+mins+':'+secs);
}
setInterval(function(){
millis += 1000;
displaytimer();
}, 1000);
答案 4 :(得分:0)
我在JSFiddle中添加了Mariusnn解决方案和Oerd建议,在Jquery中将val更改为文本。
HTML文件 <body>
<h1 style="color:blue">Time</h1>
<h2><span class="responseTime" style="color:indianred"></span>
</h2>
</body>
` var startTimer;
startTimer = function() {
var time;
time = 0;
setInterval((function() {
var hour, min, sec, str;
time++;
sec = time % 60;
min = (time - sec) / 60 % 60;
hour = (time - sec - (min * 60)) / 3600;
count = hour + ':' + ('0' + min).slice(-2) + ':' + ('0' + sec).slice(-2);
$('.responseTime').text(count);
}), 1000);
};
$( document ).ready(function() {
startTimer();
});
`