简体   繁体   English

Javascript倒数计时器未重置

[英]Javascript Countdown Timer does not reset

I am writing a quiz style application where each question has a countdown timer running. 我正在编写一个测验样式的应用程序,其中每个问题都有一个倒计时计时器运行。 At the end of the countdown the application moves on to the next question. 倒计时结束时,应用程序继续下一个问题。 However, on answering the question the timer is supposed to reset. 但是,在回答该问题后,计时器应该重置。 It does not do this and instead creates a new timer, so once the first timer reaches zero it moves on to the next question, which it shouldn't do. 它不会执行此操作,而是创建一个新计时器,因此,一旦第一个计时器达到零,它将继续执行下一个问题,这是不应该的。 I am using the countdown timer from http://www.gieson.com/Library/projects/utilities/countdown/ and my code is as follows: 我正在使用http://www.gieson.com/Library/projects/utilities/countdown/中的倒数计时器,我的代码如下:

<script type="text/javascript">
    window.countdown = '';

    function resetTimer()
    {
        window.countdown = new Countdown({ 
            time: 30,
            inline: true,
            rangeHi  : "minute",
            target: "timer",
            width: 50,
            height: 30,
            onComplete : skipQuestion
         });
    }
</script>

Everytime a question is answered (or when the timer runs out and skipQuestion() is called), I call the function resetTimer(). 每次回答问题时(或者当计时器用完并调用skipQuestion()时),我都会调用函数resetTimer()。 My javasscript is not that great, so instead of trying to reach the developer of the countdown timer, I was hoping that the vast collection of knowledgeable people on here might give me an indication of how to get this working. 我的Javasscript并不是很好,所以我没有试图与倒计时计时器的开发人员接触,而是希望这里的大量知识渊博的人可以为我提供如何工作的指示。

Thank you in advance. 先感谢您。

You should call 你应该打电话

window.clearInterval(window.countdown);

to stop the running timer. 停止正在运行的计时器。 Your code creates a new time anytime you call resetTimer(), so your function should be called createTimer(); 您的代码每次调用resetTimer()都会创建一个新时间,因此您的函数应称为createTimer();。

I found the solution. 我找到了解决方案。 Simply calling window.countdown.init() resets the timer. 只需调用window.countdown.init()即可重置计时器。

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

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