-2

这对你们大多数人来说一定很简单,但对我来说不是,所以任何帮助都将不胜感激。如何更改函数返回部分的变量之一?我只是添加变量clock =并像这样添加下面的if语句吗?

if (ms  > than 86400000) {clock =  days + " " + hours}; 

还是这样?

if (ms > than 3600000) return clock : hours + ":" + minutes. 

这是我到目前为止所拥有的。

if (DateDiff("ms", now, TestDue) >= 0) {
    var Date1 = new Date();
    var Date2 = Date.parse(Date1);
    var TestLate1 = Date.parse(TestLate);
    var TestDue1 = Date.parse(TestDue);
    var TestLate2 = convertMS(TestLate1 - Date2).clock;
    var TestDue2 = convertMS(TestDue1 - Date2).clock;
    var TestDiff = convertMS(TestLate1 - TestDue1).clock;
    tto = "../Images/Blue-120-Button.png";
    ttt = "Test Start " + TestDue2;
    ttm = "../Images/Blue-120-ButtonMouseOver.png";
    ttd = "Test will start in " + TestDue2 + ", will be due in " + (TestDiff) + " after that and will be late on " + TestLate + ".";
    C3color = "#FFFFFF";
    C3Mcolor = "#FFFF00";
    ASTRIS = "../Images/BlueTestB.png";
    ASTRIS2 = "../Images/BlueTestB2.png";

所以这是我需要在不同时间实现的功能的一部分。

function convertMS(ms) {
    days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
    hours = Math.floor((ms % 86400000) / 3600000), // 1 Hour = 3600000 Milliseconds
    minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
    seconds = Math.floor(((ms % 360000) % 60000) / 1000) // 1 Second = 1000 Milliseconds

    if (minutes.toString().length == 1) {
        minutes = "0" + minutes
    };
    if (seconds.toString().length == 1) {
        seconds = "0" + seconds
    };

//need to change the conditions with if statements for clock//
    return {
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds,
        clock: days + " " + hours + ":" + minutes + ":" + seconds
    };
}
4

3 回答 3

2

那怎么办:

function convertMS(ms) {
    days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
    hours = Math.floor((ms % 86400000)/ 3600000), // 1 Hour = 3600000 Milliseconds
    minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
    seconds = Math.floor(((ms % 360000) % 60000) / 1000); // 1 Second = 1000 Milliseconds

    if (minutes.toString().length == 1 ) {minutes = "0" + minutes};
    if (seconds.toString().length == 1 ) {seconds = "0" + seconds};

    var clock = '';
    if(ms > 86400000) {
        clock = days + " " + hours;
    } else if(ms > 3600000 && ms < 86400000) {
        clock = hours + ":" + minutes;
    } else if(ms > 60000 && ms < 3600000) {
        clock = minutes + ":" + seconds;
    } else {
        clock = seconds;
    }

    return {
        days : days,
        hours : hours,
        minutes : minutes,
        seconds : seconds,
        clock :  clock
    };
}
于 2013-08-18T07:31:17.490 回答
1

认为您正在寻找三元运算符。这可能会为您指明正确的方向:

return  ms > 86400000 ? days + " " + hours : hours + ":" + minutes

或类似的东西。在这里查看更多信息,您应该能够轻松地将其适应您的代码。

于 2013-08-18T07:31:52.397 回答
1

使用单独的变量来格式化时间,您可以在返回最终对象之前尽可能多地对其进行修改。

function convertMS(ms) {
    var days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
    hours = Math.floor((ms % 86400000)/ 3600000), // 1 Hour = 3600000 Milliseconds
    minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
    seconds = Math.floor(((ms % 360000) % 60000) / 1000) // 1 Second = 1000 Milliseconds

    if (minutes.toString().length == 1 ) {minutes = "0" + minutes}
    if (seconds.toString().length == 1 ) {seconds = "0" + seconds}

    var clockFormat;

    if( days > 0 ) {
        clockFormat = days + " " + hours;
    }
    else if( hours > 0 ) {
        clockFormat = hours + ":" + minutes;
    }
    else {
        clockFormat = minutes + ":" + seconds;
    }

    return {
        days : days,
        hours : hours,
        minutes : minutes,
        seconds : seconds,
        clock : clockFormat
    };
}
于 2013-08-18T07:45:32.133 回答