Skip to content
/ timercpp Public
forked from 99x/timercpp

Javascript like setTimeout and setInterval for c++ developers

License

Notifications You must be signed in to change notification settings

csuwu/timercpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

timercpp

Javascript like timer for c++ developers

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

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

About

Javascript like setTimeout and setInterval for c++ developers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%