斯威夫特:每10秒重新加载alamofire请求

时间:2016-06-24 02:52:12

标签: ios swift alamofire

我正在尝试从alamofire请求获取信息,我希望每10秒更新一次信息。换句话说,我希望它能够刷新。所以我希望它每10秒重复一次http请求。有谁知道怎么做?

3 个答案:

答案 0 :(得分:3)

 override func viewDidLoad() {
  super.viewDidLoad()
  var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self,selector: "execute", userInfo: nil, repeats: true)

}
// method

 func execute() {
 // do logic here
 }

更多ablout NSTImer http://www.ios-blog.co.uk/tutorials/swift/swift-nstimer-tutorial-lets-create-a-counter-application/

答案 1 :(得分:1)

查看NSTimer

示例:

NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(yourUpdatedFunctionHere), userInfo: nil, repeats: true)

答案 2 :(得分:0)

Swift 4 & 5

override func viewDidLoad() {
    super.viewDidLoad()
    _ = Timer.scheduledTimer(timeInterval: 5.0,
                             target: self,
                             selector: #selector(execute),
                             userInfo: nil,
                             repeats: true)
}

@objc func execute() {
    print("Hello")
}

这将每5秒执行一次