我知道如何启动NSTimer并且我给出了此代码,但现在我想在3或4分钟后停止NSTimer但是我该怎么做
我知道如何给NSTimer但是如何在3分钟后停止 需要一些帮助
# start the ssh-agent in the background
eval "$(ssh-agent -s)"
# Agent pid 59566
ssh-add -l
# 2048 a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/you/.ssh/id_rsa (RSA)
答案 0 :(得分:1)
节省启动计时器的时间
-(void)updateUIinMainThread:(NSTimer *)timer
{
NSDate *timeNow = [NSDate date];
NSTimeInterval timespan = [timeNow timeIntervalSinceDate:savedTime];
if(timesapn>180)
{
[timer invalidate];
}
}
并且在函数 updateUIinMainThread:将当前时间与保存时间进行比较。如果结果大于180秒,请停止计时器。
<?php
foreach ($oneimg as $oneimg_new)
{
?>
<div class="thumbnail">
<img src="<?php echo HTTP_IMAGES_PATH_UPLOADIMAGE; ?><?php echo $oneimg_new['images']?>"
alt="" class="img-responsive" style="height:180px; width:100%"/>
</div>
<?php
}
?>
答案 1 :(得分:1)
宣言:
int totalSeconds;
NSTimer *twoMinTimer;
代码:
- (void)timer {
totalSeconds--;
if ( totalSeconds == 0 ) {
[twoMinTimer invalidate];
//Timer stops after 2 minute from this you can do your stuff here
}
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
totalSeconds = 120;
twoMinTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timer)
userInfo:nil
repeats:YES];
}
希望这有帮助
答案 2 :(得分:0)
以下代码可能会对您有所帮助:
步骤1:声明以下实例变量
@interface yourclass:NSObject {
NSTimer* myTimer;
NSDate *initialDate;
}
第2步:在你想要的课程中创建myTimer:
myTimer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(callByTimer) userInfo:nil repeats:YES];
第3步:实现callByTimer方法:
- (void)callByTimer{
// get initial date at very first call by myTimer
if (!initialDate)
{
initialDate = [NSDate date];
}
// get current date
NSDate *currentDate = [NSDate date];
// get seconds between currentdate and initial date
NSTimeInterval secondsBetween = [currentDate timeIntervalSinceDate:initialDate];
// convert seconds into minutes
NSInteger minutes = secondsBetween/60;
// check if minutes is greater than or equal to 3 then invalidate myTimer and assign nil to initialDate variable
if (minutes>=3)
{
[myTimer invalidate];
initialDate = nil;
}}