All Projects → 99x → Timercpp

99x / Timercpp

Licence: mit
Javascript like setTimeout and setInterval for c++ developers

Programming Languages

cpp
1120 projects
cpp14
131 projects

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].