summaryrefslogtreecommitdiffstats
path: root/web/gui/src/dashboard.js/timeout.js
blob: 4adf9bb4ce64e27d2c761b9ebd63ef6bbf28ca7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// *** src/dashboard.js/timeout.js

// TODO: Better name needed

NETDATA.timeout = {
    // by default, these are just wrappers to setTimeout() / clearTimeout()

    step: function (callback) {
        return window.setTimeout(callback, 1000 / 60);
    },

    set: function (callback, delay) {
        return window.setTimeout(callback, delay);
    },

    clear: function (id) {
        return window.clearTimeout(id);
    },

    init: function () {
        let custom = true;

        if (window.requestAnimationFrame) {
            this.step = function (callback) {
                return window.requestAnimationFrame(callback);
            };

            this.clear = function (handle) {
                return window.cancelAnimationFrame(handle.value);
            };
        // } else if (window.webkitRequestAnimationFrame) {
        //     this.step = function (callback) {
        //         return window.webkitRequestAnimationFrame(callback);
        //     };

        //     if (window.webkitCancelAnimationFrame) {
        //         this.clear = function (handle) {
        //             return window.webkitCancelAnimationFrame(handle.value);
        //         };
        //     } else if (window.webkitCancelRequestAnimationFrame) {
        //         this.clear = function (handle) {
        //             return window.webkitCancelRequestAnimationFrame(handle.value);
        //         };
        //     }
        // } else if (window.mozRequestAnimationFrame) {
        //     this.step = function (callback) {
        //         return window.mozRequestAnimationFrame(callback);
        //     };

        //     this.clear = function (handle) {
        //         return window.mozCancelRequestAnimationFrame(handle.value);
        //     };
        // } else if (window.oRequestAnimationFrame) {
        //     this.step = function (callback) {
        //         return window.oRequestAnimationFrame(callback);
        //     };

        //     this.clear = function (handle) {
        //         return window.oCancelRequestAnimationFrame(handle.value);
        //     };
        // } else if (window.msRequestAnimationFrame) {
        //     this.step = function (callback) {
        //         return window.msRequestAnimationFrame(callback);
        //     };

        //     this.clear = function (handle) {
        //         return window.msCancelRequestAnimationFrame(handle.value);
        //     };
        } else {
            custom = false;
        }

        if (custom) {
            // we have installed custom .step() / .clear() functions
            // overwrite the .set() too

            this.set = function (callback, delay) {
                let start = Date.now(),
                    handle = new Object();

                const loop = () => {
                    let current = Date.now(),
                        delta = current - start;

                    if (delta >= delay) {
                        callback.call();
                    } else {
                        handle.value = this.step(loop);
                    }
                }

                handle.value = this.step(loop);
                return handle;
            };
        }
    }
};

NETDATA.timeout.init();