Smith AR

JS Timer Events

This extension adds JS timer events to Qt5.

Timer events function extensions

The TimerHandler is an opaque type, you can use this value for cancel the timer

function setTimeout(cb: () => any, time: number): TimerHandler;

function setInterval(cb: () => any, time: number): TimerHandler;

function clearTimeout(timer: TimerHandler): void;

function clearInterval(timer: TimerHandler): void;

NOTE: clearInterval is an synonym of clearTimeout

Example

HOW TO USE

  import QtQuick 2.0
  import org.jsmitar.timerjs 1.0

  Item {
    function foo() {
      let timer = setTimeout(_ => console.log('HELLO timer'), 100)
      clearTimeout(timer)
      clearTimeout(timer) // is safe call clearTimeout multiple times
    }

    function bar() {
      let timer = setInterval(_ => {
        console.log('HELLO timer')
        clearInterval(timer) // you can use clearTimeout too
      }, 100)
    }
  }

For limitations of Qt if you want to use Timer JS events in .mjs modules you need inject the functions inside of Qt global variable

// file: main.qml

  import QtQuick 2.0
  import org.jsmitar.timerjs 1.0

  Item {
    id: root

    Component.onCompleted: {
      Qt.setInterval = setInterval
      Qt.setTimeout = setTimeout
      Qt.clearTimeout = clearTimeout
      Qt.clearInterval = clearInterval
    }

    ...
  }
// file: my_module.mjs

  export function foo() {
    Qt.setInterval(() => console.log('HELLO timer))
  }

By clicking button above, I agree Marketplace user terms and conditions

Details
Version:
1.0
Contact:
https://github.com/jsmitar/QtTimerJS/issues
Authors:
Smith AR
License:
MIT
Created at:
2020-07-20
Platforms:
Linux
Windows 10
macOS 10.14
Supported Qt versions:
5.11-or-later
Support:
https://github.com/jsmitar/QtTimerJS/issues
Bug URL:
https://github.com/jsmitar/QtTimerJS/issues
Source repository:
https://github.com/jsmitar/QtTimerJS
Dependencies:
Qt5Qml
Qt5Quick
Qt5Test optional