Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将if语句添加到函数的返回变量_Javascript_Html - Fatal编程技术网

Javascript 如何将if语句添加到函数的返回变量

Javascript 如何将if语句添加到函数的返回变量,javascript,html,Javascript,Html,这对你们大多数人来说一定很简单,但对我来说并不简单,所以任何帮助都将不胜感激。如何更改函数返回部分的一个变量?我是否只是添加变量clock=并像这样添加下面的if语句 if (ms > than 86400000) {clock = days + " " + hours}; 还是像这样 if (ms > than 3600000) return clock : hours + ":" + minutes. 这是我到目前为止所拥有的 if (DateDiff("ms", n

这对你们大多数人来说一定很简单,但对我来说并不简单,所以任何帮助都将不胜感激。如何更改函数返回部分的一个变量?我是否只是添加变量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
    };
}
那样呢:

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
    };
}
函数转换ms(ms){
天=数学地板(毫秒/86400000),//1天=86400000毫秒
小时=数学地板((毫秒%86400000)/3600000),//1小时=3600000毫秒
分钟=数学地板((毫秒%3600000)/60000),//1分钟=60000毫秒
秒=数学地板((毫秒%360000)%60000)/1000);//1秒=1000毫秒
如果(minutes.toString().length==1){minutes=“0”+minutes};
如果(seconds.toString().length==1){seconds=“0”+seconds};
变量时钟=“”;
如果(ms>86400000){
时钟=天+小时;
}否则如果(ms>3600000&&ms<86400000){
时钟=小时+“:”+分钟;
}否则,如果(ms>60000&&ms<3600000){
时钟=分钟+“:”+秒;
}否则{
时钟=秒;
}
返回{
天:天,
小时:小时,
分钟:分钟,
秒:秒,
时钟:时钟
};
}
我想你在找三元运算符。这可能会为您指明正确的方向:

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

或者类似的。检查更多信息,您应该能够轻松地将其调整到代码中。

使用单独的变量设置时间格式,您可以在返回最终对象之前对其进行任意修改

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
    };
}

变量在哪里声明?您似乎缺少一个
var
,因为末尾有逗号。。。还有分号,在一个不同的函数中,你就是这样做的。你能展示一下你遇到问题的代码吗?在不同的函数中你是什么意思
var
创建在该函数之外不可用的局部变量。你所做的基本上是创造一堆全局的;它们不是相同的变量。到目前为止,它返回的是天、小时、分钟和秒,但我需要它为不同的时间跨度返回不同的时间格式。如果它超过60000毫秒,我需要它表示分钟+秒,如果它超过0毫秒,我需要它表示秒。我刚刚更新了我的答案。它现在应该像你期望的那样工作。