我正在尝试从alamofire请求获取信息,我希望每10秒更新一次信息。换句话说,我希望它能够刷新。所以我希望它每10秒重复一次http请求。有谁知道怎么做?
答案 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秒执行一次