/Timer

A javascript Ticker which ticks well in inactive tab.

Primary LanguageJavaScriptMIT LicenseMIT

Enlish | 中文版

Timer

A javascript Timer which works well in inactive tab.

Why I write this library?

Most of the modern browsers throttled setTimeout/setInterval to more than 1000ms if the page's inactive. In most of the cases, it saves CPU loads while the tabs are inactive, which's a good thing. But in some extreme cases (like sending heart beating packet), it requires the page to have a stable timer.
I've tried to search if there's any methods to prevent browsers throttling timers, but so far it seems like WebWorker could be the only solution to prevent throttling.

How to use it?

User <script> tag

    <script src="path/to/your/index.web.min.js"> 

Use import/export syntax

    import Timer from 'Timer';
    // Instanize the Timer.
    let T = new Timer();

    // calibrate, reduce the time offset of asynchornaze work.
    T.calibrate();

    // The same API as window.setTimeout/window.setInterval
    T.setTimeout(fn, delay, ...params);
    T.setInterval(fn, delay, ...params);
    T.setImmediate(fn, ...params);
    T.clearTimeout(timeoutID);
    T.clearInterval(intervalID);
    T.clearImmediate(immediateID);

    // These are newly added
    T.hasTimer(timerID)
    T.clearAll()

TODO

  1. add test cases.

Lisence

MIT License
Copyright (c) 2018 Jacky Wang