Below I have a javascript count down timer which works fine:
<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);
});
Now what I thought of doing is use the example above and manipulate it to now do the opposite and create a count up timer starting with 00:00:00 (hours, mins, secs).
But issue is that it is not working. Nothing is happening. I don't know if I need an hours in the function below because as its a countup, the hours could go up to as many hours as it wants. But my question is how can the count up timer be fixed so that it is fully working?
<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);
});
UPDATE:
Also as a side note I realised that the countdown timer I believe is counting down every 2 second for every second. How can this be sorted for the count down timer and included in the count up timer?
The code below I have changed to include increment ++ and to include .val rather than .text but what is happening is that it starts with 00:00:00 and then it just drops down to 00:59:60 and then just stops:
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);
});
You could easily modify the current countdown timer like here: 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);
In jQuery, use the val('value') function to change/set the value of an input field.
Change input.text(...) to input.val(...)
Also:
seconds = seconds % 60; to bring them back to 0.data-attributes for passing values to javascript.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With