Swift计时器未遵循正确的时间间隔 [英] Swift timer not following correct interval

查看:62
本文介绍了Swift计时器未遵循正确的时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个测验应用程序,该应用程序在计时器到期时(即10秒,并且我希望 Timmer 的间隔为1秒),为每个问题设置一个计时器,它会自行重置并下一个问题被提取, Timmer 再次从10重新开始...但是我的问题是,当第一个问题被加载时,计时器没有遵循固定的时间间隔,它显示的间隔为2 ...即10,8,6 ..,然后是第二个问题,它以3秒钟的间隔跳跃,并且间隔类似地增加.

I am trying to create a quiz app which has a timer for each question when the timer expires (i.e. 10 seconds and I want Timmer to have an interval of 1 sec) it resets it self and next question is fetched and Timmer again restart from 10... But my issue is the timer doesn't follow a fixed interval when first question is loaded it shows interval of 2 ... i.e. 10,8,6 .. and then for second question it makes jump for 3 secs interval and similarly the interval increases.

var countTime = 10.0
func handleNextQuestion() throws {
            nextQuestion()
            if questionCounter == allQuestions.list.count-1{
                finishButton.isHidden = false
                nextButton.isHidden = true
                //scoreLbl.text = "\(score)"
            }
        }
        
        func nextQuestion(){
            showResultView(isCorrect: (question?.isAnswerCorrect)!)
            questionCounter = questionCounter + 1
            question = fetchQuestion()
            setQuizView(question: question!)
            
            
        }
        
        @objc func update() {
            if(countTime > 0) {
                countTime = countTime - 1
                self.countDownLabel.text = String(countTime)
            }else{
                timer.invalidate()
                countTime = 10.0
                do{
    
                    try handleNextQuestion()
    
                }
                catch{
                       moveToResultView()
                }
                
            }
        }
     
        
       
        func startTimer() {
            timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
        }
        func setQuizView(question:Question)  {
            self.countDownLabel.text = "10"
          startTimer()
            startTimer()
            questionLabel.text =  question.questionText
            ansLbl1.text =  question.answer1
            ansLbl2.text =  question.answer2
            ansLbl3.text =  question.answer3
            ansLbl4.text =  question.answer4
            
            if question.selectedAnswer == Constants.DEFAULT_ANSWER {
                for checkBoxItem in checkBoxlist{
                    checkBoxItem.isChecked = false
                }
                
            }
            
        }
        

推荐答案

func setQuizView(question:Question)

调用 startTimer()两次.我认为这可以解释这种行为.在每个新问题上,计时器都会启动两次,但只会使计时器失效一次,因此秒数会增加一个检查问题.

calls startTimer() twice. I think that explains the behaviour. On each new question the timer is started twice but only invalidated once so the seconds jump by an extra one exach question.

只需删除对 startTimer()

这篇关于Swift计时器未遵循正确的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆