Showing posts with label schedule. Show all posts
Showing posts with label schedule. Show all posts

Tuesday, 4 June 2013

How to call a function using NSTimer: Xcode

NSTimer can be used to call functions with a scheduled delay. You can control the interval and repeat mode (whether the function need to be called per every delay intervals or not) also. The syntax is very simple, and is given below:

Syntax:

         [NSTimer scheduledTimerWithTimeInterval:0.60
                                                                   target:self
                                                                   selector:@selector(myFunction)
                                                                   userInfo:nil
                                                                   repeats:NO];

Friday, 31 May 2013

How to call a function directly/with scheduler and cancel scheduler: cocos2d

In cocos2d, the function call can be either direct call without any delay, or scheduled call with a delay.

In direct call, the syntax is as follows:

      [self functionName];                                    // to call a function without delay


In scheduled function call, the syntax is:

    [self schedule:@selector(functionName) interval:10];    // to call a function with delay = 10min.s.

Note:   If the function is called with a selector with specified time interval(say 10min, as above), then it will get called in every repeated,specified time intervals(in the above case, it will trigger in every 10 min.s). So, if you need to stop the function getting called repeatedly, you can simply use the following line inside the called function at it's beginning.



       [self unschedule:_cmd];           

       example:                  
              function (normal):

                  -(void)myFunction{
                          .............................
                          .............................
                          .............................
                    }









              scheduled call:

                  [self schedule:@selector(myFunction) interval:2];

              to stop the function from getting called per each 2 mins, 
              rewrite the function as follows:

                     -(void)myFunction{
                          [self unschedule:_cmd];           // this will unschedule the scheduler
                          .............................
                          .............................
                          .............................

                    }