为什么我的定时器每一秒倒计时3? [英] Why is my timer counting down by 3 every second?

查看:140
本文介绍了为什么我的定时器每一秒倒计时3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的倒计数通过3.我引发了计时器定时器的 viewDidLoad中的方法,在那里我也解析查询时间变量。

I have a timer that's counting down by 3. I'm triggering the timer in the viewDidLoad method where I'm also querying time variables from Parse.

下面是我的code其中计时功能是:

Here's my code where the timer function is:

    var createdAt = object.createdAt
            if createdAt != nil {

                let calendar = NSCalendar.currentCalendar()
                let comps = calendar.components([.Hour, .Minute, .Second], fromDate: createdAt as NSDate!)
                let hour = comps.hour  * 3600
                let minute = comps.minute * 60
                let seconds = comps.second


                self.timerInt.append(hour + minute + seconds)

                var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDown"), userInfo: nil, repeats: true)


            } 

            }

        }


    })
    self.tableView.reloadData()

}

我试图改变周围的 self.tableView.reloadData(),因为我假定这是一个reloadData问题。

I've tried changing around the self.tableView.reloadData() because I'm assuming that it's a reloadData problem.

下面是我的倒计时功能:

Here is my countdown function:

       func countDown() {
        //timerInt is an array where I'm storing the Int values.
        for i in 0 ..< timerInt.count {

            let hours = timerInt[i] / 3600
            let minsec = timerInt[i] % 3600
            let minutes = minsec / 60
            let seconds = minsec % 60
       print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
          timerInt[i]--

        }
            self.tableView.reloadData()
}

我想这一切都在发生的的的cellForRowAtIndexPath 的方法..

下面是的cellForRowAtIndexPath 的方法:

  let hours = timerInt[indexPath.row] / 3600
    let minsec = timerInt[indexPath.row] % 3600
    let minutes = minsec / 60
    let seconds = minsec % 60

  //defining the time 

    myCell.secondLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)

             //formatting the time


    return myCell

任何想法是什么使得它引发的3倍?

Any ideas as to what is making it trigger 3x?

推荐答案

愚蠢的我。意识到,如果我从查询解析对象3秒倒计时通过3.如果我查询4,则秒4.倒计时

Stupid of me. Realized that if I'm querying 3 objects from Parse the seconds count down by 3. If I'm querying 4, then the seconds count down by 4.

问题是在那里我触发计时器方法:

The problem was where I'm triggering the timer method:

VAR定时器= NSTimer.scheduledTimerWithTimeInterval(1,目标:自我,选择:选择(倒计时),用户信息:无,重复:真)

我需要之外触发它我询问在何处。因为如果它的触发那里,然后它被触发与我查询对象的量。

I need to trigger it OUTSIDE of where I'm querying. Because if it's triggered there, then it's been triggered with the amount of objects I'm querying.

现在我把定时变量这样和它的作品:):

Now I put the timer variable like this and it works :) :

      var createdAt = object.createdAt
            if createdAt != nil {

                let calendar = NSCalendar.currentCalendar()
                let comps = calendar.components([.Hour, .Minute, .Second], fromDate: createdAt as NSDate!)
                let hour = comps.hour  * 3600
                let minute = comps.minute * 60
                let seconds = comps.second


                self.timerInt.append(hour + minute + seconds)


            }


            }

        }

         var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDown"), userInfo: nil, repeats: true)

    })
    self.tableView.reloadData()

}

请注意,在} 与其他瓶盖内我的 viewDidLoad中的。

Note, the } are from other closures within my viewDidLoad.

这篇关于为什么我的定时器每一秒倒计时3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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