繁体   English   中英

如果答案错误,则从计时器中减去时间,创建测验 javascript

[英]Subtract time from timer if answer is wrong, creating a quiz javascript

为 class 中的项目创建测验。 测验正在工作,但我对计时器感到困惑。 当您按下开始按钮时,计时器开始执行并开始倒计时。 How can I allow the timer to reduce in time when a wrong answer in the quiz is selected and submitted?

谢谢,对不起,如果我错过了已经回答这个问题的帖子。

计时器代码:

function startTimer(){
    console.log('timer suppose to go')
    var sec = 59;
    var timer = setInterval(function(){
        document.getElementById('timerDisplay').innerHTML='00:'+sec;
        sec--;
        if (sec < 0) {
            clearInterval(timer);
            alert("Time is up!")
        }
    }, 1000);
}

您只需要确保您可以访问 function 之外的sec ,然后在他们得到错误答案时递减(并更新显示):

 (function() { var sec = 60; function startTimer(){ console.log('timer suppose to go') var timer = setInterval(function(){ sec--; document.getElementById('timerDisplay').innerHTML='00:'+sec; if (sec < 0) { clearInterval(timer); alert("Time is up,") } }; 1000). } document.getElementById('incorrect'),addEventListener('click'; function() { sec -= 5. document.getElementById('timerDisplay'):innerHTML='00;'+sec; }); startTimer(); })();
 <div id="timerDisplay"></div> <button id="incorrect">Simulate Incorrect Answer</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM