I've got a program in which I want to code a timer which checks wether the user is idle or not. For that I wrote following code:
if (!idleTimer) {
NSLog(#"make");
idleTimer=[[NSTimer alloc]initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:10.0]interval:10 target:self selector:#selector(idleTimerExceeded) userInfo:nil repeats:NO];
NSLog(#"madetimer with: %f, %#", idleTimer.timeInterval, idleTimer.fireDate);
}else {
NSLog(#"no reset timer: %f", idleTimer.timeInterval);
if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < 9) {
NSLog(#"reset");
[idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
}
But somehow my logs show that the interval is always 0.0000 . Which means that something is wrong here. Can anybody help me?
ive never seen a timer look so complicated. try this:
write a method that checks if the user is idle (lets say idleChecker)
then make the timer repeatable and calls that method idleChecker
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
remember to declare the idleChecker method in the .h file
take note if u want to stop the timer event then you need to maintain a reference to it
NSTimer aTimer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
then to stop it
[aTimer invalidate];
as for checking if its running i would just stick a nslog message in there stating something like "check for idle user"
Related
I'm using XCode 5. I'm making a game using Cocos2D. And want some functions to do repeatedly in sequence and one after another. Due to many actions performed at a time, so wants a delay too (by which no action is interfered by the next one and not cancelling the effect of previous one or rescheduling and perform the function immediately).
My interface freezes and in Debug Navigator, CPU consumption reaches between 99-100% and memory gradually increasing and remain increasing. I put all exceptions breakpoint, too. But no exception arises.
I'm using this code
-(void)threadMainRunLoop
{
BOOL exitNow = NO;
int loopCount = 1;
[self function1];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
// Add the exitNow BOOL to the thread dictionary.
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
[threadDict setValue:[NSNumber numberWithBool:exitNow] forKey:#"ThreadShouldExitNow"];
// Install an input source.
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(function2) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function3) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function4) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:#selector(function5) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:#selector(function6) userInfo:nil repeats:NO];
while (loopCount <= 4 && !exitNow)
{
// Do one chunk of a larger body of work here.
// Change the value of the moreWorkToDo Boolean when done.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(function2) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function3) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function4) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:#selector(function5) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:#selector(function6) userInfo:nil repeats:NO];
// Run the run loop but timeout immediately if the input source isn't waiting to fire.
[runLoop runUntilDate:[NSDate dateWithTimeInterval:5.0 sinceDate:[NSDate date]] ];
// Check to see if an input source handler changed the exitNow value.
exitNow = [[threadDict valueForKey:#"ThreadShouldExitNow"] boolValue];
++loopCount;
}
}
And calling this function in ccTouchesEnded:withEvent:.
I followed this code from Thread Management of "Listing 2-3 Checking for an exit condition during a long job".
Is my NSRunLoop make my game/interface to freeze???
Remember device in not hanged. And function1-6 are updating UI. And also when the game will freeze is not expected, may be after some time or after a long time or even may not freeze.
Thanks in advance.
cocos2d is not thread-safe, so if any of these scheduled functions perform cocos2d actions or modify nodes on a background thread there will be problems. Instead use cocos2d's builtin scheduleSelector:interval: methods.
A specific issue here is the while loop. You call runUntilDate which blocks the main thread for 5 seconds, and you do that up to 4 times. Which means cocos2d and ui get to update and draw their state once every 20 seconds. No wonder the app freezes.
If you want to do some multithreading, use performSelectorInBackground or grand central dispatch. Run loops and threads are too low level if you don't control the app's entire run loop and all threads.
This is what I want to do:
I want a timer, to fire a method and then, in the end of this method, be toggled off, and turn on an other timer on another method, and then entering a loop.
So what are the codes used to toggle between on and off the timer on a method?
In Delphi I use:
timer.enable:=True; // timer.enable:=False;
Are there a similar way to do it on objective-c?
I'm using Xcode 4.4
Thanks!
To turn the timer off, call invalidate on your timer like so:
[yourTimer invalidate]
And then to start a new one:
NSTimer *newTimer;
newTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 //Every how many seconds
target:self
selector:#selector(methodToCall)
userInfo:nil
repeats:YES];
Assuming your NSTimer is called "timer", you can use...
[timer invalidate]
to stop the timer. To make a timer pass a message to it's target method instantly, use
[timer fire]
To start a timer, you use one of the constructor methods listed in the documentation (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html) such as
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(doThisWhenTimerFires:) userInfo:nil repeats:NO]
- (void)doThisWhenTimerFires:(NSTimer *)timer
{
//code here
}
I'm looking for the cleanest way to have a countdown timer fire when a user hits the 'take picture' button. Is there any easy way to do this?
One solution I'm thinking of is to just have a label that updates every second, but is there a way to get it working like Photobooth?
Also, at the last second before the photo is about to be taken I'd like for an image to be displayed briefly while the image is being taken. How can I go about this?
Any help would be great, thanks!
- (IBAction)takePicture:(id)sender {
theTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(updateLabel:) userInfo:nil repeats:NO];
}
- (void)updateLabel:(NSTimer *)timer {
_timeLabel.text = [NSString stringWithFormat:#"%d", time];
time = time - 1;
if (time == 0) {
[theTimer invalidate];
[_timeLabel performSelector:#selector(setText:) withObject:#"Photo taken!" afterDelay:1.0];
//Code for image shown at last second
} else {
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateLabel:) userInfo:nil repeats:NO];
}
}
Hope this helps ;)
I want to create a timer for example to count 2 sec and after each second an nslog type for example 1 sec passed
any suggestion to do that
yes there is NSTimer, use it as -
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector() userInfo:nil repeats:NO];
What you're after is NSTimer.
Which, I can't not point out, even a cursory search of the framework docs would have turned up. Laziness IS one of the three Virtues of the Programmer, but c'mon.
You can call your NSTimer like this
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(changeValue) userInfo:nil repeats:YES];
changeValue function can be like
-(void)changeValue{
NSLog("calling function after every two seconds");
}
You might want to use NSTimers timerWithTimeInterval:target:selector:userInfo:repeats:
method. Point that towards some object that implements a selector which prints your log entries.
Yes, ios having timer which is used for periodically call method or button touchup event. we can use it like following:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(Your_target_Method)
userInfo:nil
repeats:NO];
NSInteger timer=0;
now call method every 1 sec
-(void)Your_target_Method{
timer=timer+1;
NSLog(#"Timer:%ld",timer);
}
Furthermore, visit https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Timers/Timers.html
I am new to iPhone,
What I want is that my timer call a function every second but for the very first time it must call the function at 2 second.
Any help will be really appreciated. Thanks
take a variable
BOOL isNotFirstTime;
And in the function that gets triggered after each 4 secs add the following code at the starting
-(void)AfterOneSecondsFuction
{
if(!isNotFirstTime)
{
isNotFirstTime=YES;
return;
}
//YOUR CODE HERE...
}
This is how you give a call to the function
NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(AfterOneSecondsFuction) userInfo:nil repeats:YES];
First schedule a timer and have a BOOL variable say isFirstTime set as YES;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doSomething) userInfo:nil repeats:YES];
and in
- (void)doSomething
{
if(isFirstTime)
{
isFirstTime = NO;
return;
}
// Do something
}