how to stop timerfunc?

hei, everybody, I want to know how to stop the timerfunc from running? I can stop idlefunc easily, but I am not sure about the timerfunc.

You mean in glut? Try to be more specific, as to which api are you using. If glut it is, can’t you pass a NULL pointer to the timerfunc callback? Also I think that the timer only runs once, so you’d have to reset it every time. e.g. if you wanted something to happen every 1 second:

void timer()
{
  //do something
  glutTimerFunc(timer, 1000);
}

If the last line isn’t used the timer will only run for one time.

Ohh, I am sorry for that. Ya, it is glut anyway. But how if I want to run the timer, say for 5 times only? For glutidlefunc, I can stop by just passing NULL parameter to it, but it does not work the same when I attempt in gluttimerfunc.

Hi!, my two solutions are:

Solution 1: If you don’t use the argument of `void timer(int)’:

 
#define ITERATIONS 5
#define TIME_INTERVAL 1000

void timer(int argument)
{
   // Do something here.
   if (argument > 0)
      glutTimerFunc(TIME_INTERVAL, timer, argument - 1);
}

void init()
{ glutTimerFunc(TIME_INTERVAL, timer, ITERATIONS); }

Solution 2: if you need the argument of `void timer(int)’:

// A global variable
int iterations = 5;
#define TIME_INTERVAL 1000

void timer(int argument)
{
   // Do something here.
   if (iterations > 5) {
      glutTimerFunc(TIME_INTERVAL, timer, iterations);
      --iterations;
   }
}

void init()
{ glutTimerFunc(TIME_INTERVAL, timer, iterations); }

Thanks a lot, I really appreciate your suggestions. Thanks again!