Skip to content
/ timercpp Public

Javascript like setTimeout and setInterval for c++ developers

License

Notifications You must be signed in to change notification settings

99x/timercpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Javascript like setTimeout and setInterval for c++ developers

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages