Need help with timercpp?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

99x
140 Stars 37 Forks MIT License 18 Commits 6 Opened issues

Description

Javascript like setTimeout and setInterval for c++ developers

Services available

!
?

Need anything else?

Contributors list

# 43,472
Kuberne...
Amazon ...
acm
Ada
16 commits

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 
#include "timercpp.h"

using namespace std;

int main() { Timer t = Timer();

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

t.setTimeout([&amp;]() {
    cout &lt;&lt; "Hey.. After 5.2s. But I will stop the timer!" &lt;&lt; endl;
    t.stop();
}, 5200); 



cout &lt;&lt; "I am Timer" &lt;<endl while 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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.