點擊「startButton」按鈕後,觸發計時器每隔5秒執行一次「myCalculation」。 問題:myCalculation方法的執行時間大約需要5-10s才能完成目標c NSTimer間隔時間設置
Q1.如何爲定時器設置合適的時間間隔? (我認爲設置時間間隔爲10s太長,如果retrieveAndUpdateNews花費5s完成,程序將獲得5s空閒)
在當前情況下,儘管myCalculation需要10s才能完成,但計時器將跳過執行retrieveAndUpdateNews並觸發retrieveAndUpdateNews 5秒後。
Q2.Are這是我的應用程序的一個潛在問題? 謝謝你的時間。
-(IBAction) startButton : (id)sender{
NSTimer * myTimer;
timer = [NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:@selector(retrieveAndUpdateNews)
userInfo:nil repeats:YES];
}
-(void)retrieveAndUpdateNews{
//some calculation spend 5 - 10 sec.
}
爲什麼使用定時器?你在做什麼需要5到10秒?每次計算完成後,你真的想要開始5秒嗎? – Wain
非常抱歉我的錯字,「myCalculation」應該改爲「retrieveAndUpdateNews」。我的任務是讓用戶按「startButton」,然後程序將保持從Web服務器檢索數據並將數據更新爲前端標籤。完成取決於服務器的響應時間需要5-10s。 –