• 10
• 9
• 12
• 14
• 14

# Newbie clock thing

This topic is 4499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a problem on C++. I want to "refresh" a function after 30 sec. I found this time function: --------------------------------- do { end=clock(); } while ((end-start)/CLK_TCK<30); ---------------------------------- that function should run 30 sec; Bu my function is: ------------------------------------- status() { cout<<money; cout<<xp; cout<<citizens; } -------------------------------------- So what I want is that status(); to refresh after 30 sec; (will display money,xp,citizens for 30sec and after that time should draw again same thing) I've tried to implemet the time function over my function bu I dont really know how. Sorry for this dumb question but I'm a newbie. Thanks

##### Share on other sites
now = time_in_seconds( );while( now - last_time > 30 ) {  call_function( );  last_time += 30;}

##### Share on other sites
That while loop will "do nothing" for 30 seconds, yes - while consuming all the CPU power to keep asking what time it is :) What you want to do is leave your refresh() function as is, and in a main "game loop", call it when an appropriate amount of time has elapsed.

int main() {  int start = clock();  while (gameShouldRun) {    doSomethingUseful();    sleep(1); // play nice with other processes    if ((clock() - start) / CLK_TCK < 30) {      // 30 seconds have elapsed, so we call the special update function      status();      // and "reset" the timer.      start = clock();    }  }}

##### Share on other sites
Thanks a lot ToohrVyk and Zahlman. I should update my code
Bye