diff --git a/timercpp.h b/timercpp.h index 46d3226..74a44e0 100644 --- a/timercpp.h +++ b/timercpp.h @@ -1,3 +1,6 @@ +#ifndef __99XT_TIMERCPP +#define __99XT_TIMERCPP + #include #include #include @@ -5,37 +8,34 @@ class Timer { bool clear = false; - public: - void setTimeout(auto function, int delay); - void setInterval(auto function, int interval); - void stop(); - -}; - -void Timer::setTimeout(auto function, int delay) { - this->clear = false; - std::thread t([=]() { - if(this->clear) return; - std::this_thread::sleep_for(std::chrono::milliseconds(delay)); - if(this->clear) return; - function(); - }); - t.detach(); -} - -void Timer::setInterval(auto function, int interval) { - this->clear = false; - std::thread t([=]() { - while(true) { +public: + void setTimeout(auto function, int delay) { + this->clear = false; + std::thread t([=]() { if(this->clear) return; - std::this_thread::sleep_for(std::chrono::milliseconds(interval)); + std::this_thread::sleep_for(std::chrono::milliseconds(delay)); if(this->clear) return; - function(); - } - }); - t.detach(); -} + function(); + }); + t.detach(); + } + + void setInterval(auto function, int interval) { + this->clear = false; + std::thread t([=]() { + while(true) { + if(this->clear) return; + std::this_thread::sleep_for(std::chrono::milliseconds(interval)); + if(this->clear) return; + function(); + } + }); + t.detach(); + } + + void stop() { + this->clear = true; + } +}; -void Timer::stop() { - this->clear = true; -} \ No newline at end of file +#endif // __99XT_TIMERCPP