I’ve written a timer class. After starting the timer, I would like to know if 20 seconds has been passed or not, if it is, I would like to call a function or perform a block of code. That class doesn’t work but I Don’t know why .
EDIT: By it doesn’t work I mean that isTimeTout(seconds) always return true; I would like just to see if few seconds has been passed, and based on that do an action.
class timer {
private:
unsigned long begTime;
public:
void start() {
begTime = clock();
}
unsigned long elapsedTime() {
return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
}
bool isTimeout(unsigned long seconds) {
return seconds >= elapsedTime();
}
};
Since you’re on Windows, you can stick with using
clock().The error is here:
it should be:
What you have right now will return
truewhen less than 20 seconds has elapsed. Flipping the comparison should fix it.