如何在Javascript / nodejs中声明实例变量?

时间:2019-11-23 21:43:59

标签: javascript node.js

我正在按小时运行一个守护程序。此代码应跟踪所有孤立的客户端,如果一个客户端保持孤立12个小时,则应将其删除。该代码以每小时的频率运行,因此我需要每小时保存一次数据,以便在下一个小时可用。由于我是Java语言的新手,所以我尝试声明一个全局变量 deleteOrphanedClients = new Map(); 。 我的以下全局变量 deleteOrphanedClients 是否将被保存并在每次触发代码时可用?

var deleteOrphanedClients = new Map();

// Delete idle client without any parent.  
exports.cleanupClients = function (clients, callback) {
    for (int i = 0; i < clients.length; i++) {
        // this checks if client is orphaned.
        if (clients[i]["parent"] == undefined) {
            if (deleteOrphanedClients[clients[i]] == undefined) {
                deleteOrphanedClients[clients[i]] = new Date();
            }
        }
    }

    // delete all clients older than 12 hours from deleteOrphanedClients
};

var manager = function (cb) {

    async.series(
        [
            function (callback) {

                async.series(
                    [
                        function (cbi) {
                            // get clients.
                        },
                        function (cbi) {
                            cleanupClients();
                        },
                    ],
                    function (err, results) {
                        callback(err, results);
                    }
                );
            }
        ],
    );
};

exports.manager = manager;

var randomInt = function (low, high) {
    return Math.floor(Math.random() * (high - low + 1) + low);
};

var pause = randomInt(0, 60);
setTimeout(
    function () {
        setInterval(manager, 36000 * 1000,
            function (err, results) {
                if (err) {
                    logError(err);
                }
            });
    }, pause * 1000);```










0 个答案:

没有答案