Skip to content

Latest commit

 

History

History
75 lines (52 loc) · 1.28 KB

README.md

File metadata and controls

75 lines (52 loc) · 1.28 KB

timercpp

Javascript like timer for c++ developers

This header only library has js equivalent setTimeout() and setInterval() for c++.

DISCLAIMER - This implementation uses threads not a queue

setTimeout(auto function, int delay)

Timer t = Timer();
t.setTimeout([&]() {
    cout << "Hey.. After 1s." << endl;
}, 1000); 

setInterval(auto function, int interval)

Timer t = Timer();
t.setInterval([&]() {
    cout << "Hey.. After each 1s..." << endl;
}, 1000); 

Sample Program

#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!

How to compile sample program?

g++ sample.cpp -std=c++14 -pthread

License

MIT