<input class="btnAction" onclick="countdown(time)" type="submit" name="finish" id="finish" style="display:none;">
//trying to get the time when user submits quiz
function countdown(minutes) {
var seconds = 60;
var mins = minutes
var time = document.getElementById("timer").innerText;
function tick() {
var counter = document.getElementById("timer");
var current_minutes = mins-1
seconds--;
//return time;
//alert(time);
counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if( seconds > 0 ) {
setTimeout(tick, 1000);
} else {
if(mins > 1){
// countdown(mins-1); never reach “00″ issue solved:Contributed by Victor Streithors
setTimeout(function () { countdown(mins - 1); }, 1000);
}
else
{
alert("your time has been ended");
document.getElementById('test-form').submit();
// var time = document.getElementById("timer").innerText.split(":").map(function(val){ return parseInt(val)});
// console.log("minutes",time[0]);
// console.log("seconds",time[1]);
}
}
}
tick();
}
countdown(2);
例如,如果用户结束1分钟,我想得到剩余1分钟的测验所花费的时间我希望有这个。任何人都可以帮助我,渴望得到时间
答案 0 :(得分:1)
您可以在下面找到如何开始测验倒计时的示例。
var seconds = 300;
function secondPassed() {
var minutes = Math.round((seconds - 30)/60),
remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
if (seconds == 0) {
clearInterval(countdownTimer);
//your code here
} else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);
.timer {
width: 200px;
font-size: 2.5em;
text-align: center;
}
<div class="timer">
<time id="countdown">5:00</time>
</div>
如何获得时间
var time = document.getElementById("countdown").innerText.split(":").map(function(val){ return parseInt(val)});
console.log("minutes",time[0]);
console.log("seconds",time[1]);