Javascript like timer for c++ developers
This header only library has js equivalent setTimeout()
and setInterval()
for c++ (multithreaded).
Timer t = Timer();
t.setTimeout([&]() {
cout << "Hey.. After 1s." << endl;
}, 1000);
Timer t = Timer();
t.setInterval([&]() {
cout << "Hey.. After each 1s..." << endl;
}, 1000);
#include <iostream>
#include "timercpp.h"
using namespace std;
int main() {
Timer t = Timer();
t.setInterval([&]() {
cout << "Hey.. After each 1s..." << endl;
}, 1000);
t.setTimeout([&]() {
cout << "Hey.. After 5.2s. But I will stop the timer!" << endl;
t.stop();
}, 5200);
cout << "I am Timer" <<endl;
while(true); // Keep main thread active
}
Output
I am Timer
Hey.. After each 1s...
Hey.. After each 1s...
Hey.. After each 1s...
Hey.. After each 1s...
Hey.. After each 1s...
Hey.. After 5.2s. But I will stop the timer!
g++ sample.cpp -std=c++14 -pthread