从计时器获取毫秒

时间:2016-05-25 14:27:48

标签: javascript node.js

我正在尝试使用node.js运行app,这将显示事件结束前剩余的时间。到目前为止,我可以得到多少:小时,分钟,秒,但我也需要几毫秒。

    var lottery_data = {
    start: 0,
    endTime: 0,
    running: 0,
    roundTime: (86400 * 1000)
};

/**
 * Start the lottery
 */

lottery();

/**
 * Lottery functions and methods
 */

function lottery() {
    var functionData = {
        timeLeft: 0,
        timeLeftDetails: {
            msAll: 0,
            hours: 0,
            minutes: 0,
            seconds: 0,
            ms: 0
        }
    };

    // check for start time and define if not defined
    if (lottery_data.start == 0) {
        lottery_data.start = Math.floor(Date.now());
        lottery_data.endTime = Math.floor(Date.now()) + lottery_data.roundTime;
    }

    // how much time left to end?
    if (lottery_data.running == 0) {
        functionData.timeLeft = lottery_data.endTime - Math.floor(Date.now() / 1000); // seconds
        if (functionData.timeLeft > 1) {
            lottery_data.running = 0;

            // construct time left
            functionData.timeLeftDetails.msAll = lottery_data.endTime - Math.floor(Date.now());
            functionData.timeLeftDetails.hours = parseInt((functionData.timeLeftDetails.msAll / 3600) / 1000);
            functionData.timeLeftDetails.minutes = parseInt(((functionData.timeLeftDetails.msAll / 1000) - (functionData.timeLeftDetails.hours * 3600)) / 60);
            functionData.timeLeftDetails.seconds = parseInt(((functionData.timeLeftDetails.msAll / 1000) - (functionData.timeLeftDetails.hours * 3600)) - (functionData.timeLeftDetails.minutes * 60));

            console.log('Left hours: ' + functionData.timeLeftDetails.hours + ', mins: '
                + functionData.timeLeftDetails.minutes + ', seconds: ' + functionData.timeLeftDetails.seconds
                + ', ms: ' + functionData.timeLeftDetails.ms + ', msAll: ' + functionData.timeLeftDetails.msAll);

            // call function itself
            setTimeout(lottery, 10);
        }
    } else {
        console.log('lottery running!');
    }
}

所以我可以看到这些东西:http://pastebin.com/tYMSSUMx

但如何知道剩下多少ms?我尝试从所有ms计数中获取小时+分钟+秒数,但它没有帮助。

0 个答案:

没有答案