Wt: Wt::WTimer Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
Wt::WTimer Class Reference

A utility class which provides timer signals and single-shot timers. More...

`#include <Wt/WTimer.h>`

Inheritance diagram for Wt::WTimer:
[legend]

## Public Member Functions
| WTimer ()
| Construct a new timer with the given parent.
| ~WTimer ()
| Destuctor.
std::chrono::milliseconds | interval () const
| Returns the interval.
void | setInterval (std::chrono::milliseconds interval)
| Sets the interval.
bool | isActive () const
| Returns if the timer is running.
bool | isSingleShot () const
| Is this timer set to fire only once.
void | setSingleShot (bool singleShot)
| Configures this timer to fire only once.
void | start ()
| Starts the timer.
void | stop ()
| Stops the timer.
EventSignal< WMouseEvent > & | timeout ()
| Signal emitted when the timer timeouts.
Public Member Functions inherited from Wt::WObject
void | addChild (std::unique_ptr< WObject > child)
| Add a child WObject whose lifetime is determined by this WObject.
template<typename Child >
Child * | addChild (std::unique_ptr< Child > child)
| Add a child WObject, returning a raw pointer.
std::unique_ptr< WObject > | removeChild (WObject *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
template<typename Child >
std::unique_ptr< Child > | removeChild (Child *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
virtual const std::string | id () const
| Returns the (unique) identifier for this object.
virtual void | setObjectName (const std::string &name)
| Sets an object name.
virtual std::string | objectName () const
| Returns the object name.
void | resetLearnedSlots ()
| Resets learned stateless slot implementations.
template<class T >
void | resetLearnedSlot (void(T::*method)())
| Resets a learned stateless slot implementation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)())
| Declares a slot to be stateless and learn client-side behaviour on first invocation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)(), void(T::*undoMethod)())
| Declares a slot to be stateless and learn client-side behaviour in advance.
void | isNotStateless ()
| Marks the current function as not stateless.
template<class T >
WStatelessSlot * | implementJavaScript (void(T::*method)(), const std::string &jsCode)
| Provides a JavaScript implementation for a method.
Public Member Functions inherited from Wt::Core::observable
| observable () noexcept
| Default constructor.
virtual | ~observable ()
| Destructor.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...)) noexcept
| Protects a method call against object destruction.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...) const) const noexcept
| Protects a const method call against object destruction.
template<typename Function >
auto | bindSafe (const Function &function) noexcept
| Protects a function against object destruction.

## Static Public Member Functions
template<class T , class V >
static void | singleShot (std::chrono::milliseconds interval, T *receiver, void(V::*method)())
| This static function calls a slot after a given time interval.
template<class F >
static void | singleShot (std::chrono::milliseconds interval, const F &f)
| This static function calls a function after a given time interval.
Static Public Member Functions inherited from Wt::WObject

## Additional Inherited Members
Public Types inherited from Wt::WObject
typedef void(WObject::*) | Method()
| Typedef for a WObject method without arguments.
Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * | getStateless (Method method)
| On-demand stateless slot implementation.

## Detailed Description

A utility class which provides timer signals and single-shot timers.

To use a timer, create a WTimer instance, set the timer interval using setInterval() and connect a slot to the timeout signal. Then, start the timer using start(). An active timer may be cancelled at any time using stop().

By default, a timer will continue to generate events until you stop() it. To create a timer that will fire only once, use setSingleShot(). There is also a convience static method singleShot().

When connecting stateless slot implementations to the timeout signal, these stateless slot implementations will be used as for any other signal (when Ajax is available).

In clients without (enabled) JavaScript support, the minimum resolution of the timer is one second (1000 milli-seconds), and it is probably wise to use timers sparingly.

A WTimer is only usable inside of a Wt event loop. If you want to create a timer outside the Wt event loop, take a look at asio deadline_timer or steady_timer.

Timers are one way to provide updates of a web page without the user generating an event. Alternatively you may consider server-initiated updates, see WApplication::enableUpdates().

Usage example:

// setup a timer which calls MyClass::timeout() every 2 seconds, until timer->stop() is called.
auto timer = root()->addChild(std::make_unique<Wt::WTimer>());
timer->setInterval(std::chrono::seconds(2));
timer->timeout().connect(this, &MyClass::timeout);
timer->start();

## Member Function Documentation

## ◆ setSingleShot()

void Wt::WTimer::setSingleShot | ( | bool | singleShot | ) |

Configures this timer to fire only once.

A Timer is by default not single shot, and will fire continuously, until it is stopped.

See also
  singleShot()

## ◆ singleShot() [1/2]

template<class F >

void Wt::WTimer::singleShot | ( | std::chrono::milliseconds | interval,
|  | const F & | f )
| static

This static function calls a function after a given time interval.

This variant of the overloaded singleShot() method supports a template function object (which supports operator ()).

## ◆ singleShot() [2/2]

template<class T , class V >

void Wt::WTimer::singleShot | ( | std::chrono::milliseconds | interval,
|  | T * | receiver,
|  | void(V::* | method )() )
| static

This static function calls a slot after a given time interval.

For example, the following code will call this->doSome() after 2 seconds:

WTimer::singleShot(2000, this, &MyClass::doSome);
Wt::WTimer::singleShot
static void singleShot(std::chrono::milliseconds interval, T *receiver, void(V::*method)())
This static function calls a slot after a given time interval.
Definition WTimer.h:170

## ◆ start()

void Wt::WTimer::start | ( |  | ) |

Starts the timer.

The timer will be isActive(), until either the interval has elapsed, after which the timeout signal is activated, or until stop() is called.

## ◆ stop()

void Wt::WTimer::stop | ( |  | ) |

Stops the timer.

You may stop the timer during its timeout(), or cancel a running timer at any other time.

See also
  start()

## ◆ timeout()

EventSignal< WMouseEvent > & Wt::WTimer::timeout | ( |  | ) |

Signal emitted when the timer timeouts.

The WMouseEvent does not provide any meaningful information but is an implementation artefact.