简体   繁体   English

如何确保我的函数对我的变量有效?

[英]How do I make sure my function works for my variable?

function numberToTime(num){
var d = 0, h = 0, m = 0;
var numToMinutes = num*60;
while(numToMinutes > 59){
    numToMinutes -= 60;
    h++;
    if(h > 23){
       h-= 24;
       d++;
    }
    m = numToMinutes;
}
if( d > 0){
    return d + " days " + h + " hours " + m +" minutes ";
}else{
    return h+":"+m;
}

This code was given to me by a very nice user here on Stack Overflow. 这段代码是由Stack Overflow上的一个非常好的用户提供给我的。 Since I am very new to programming, especially JavaScript I have no idea where to put my variable. 因为我对编程非常陌生,尤其是JavaScript,所以我不知道将变量放在何处。 I have a var howLong = (0,1 * amount + 0,2 * time) I want to convert it to hours and minutes with the code above, but I don't know how to tell the function it's about var howLong . 我有一个var howLong = (0,1 * amount + 0,2 * time)我想用上面的代码将其转换为小时和分钟,但是我不知道如何告诉函数它与var howLong

Can somebody help me out? 有人可以帮我吗?

May be like this? 可能是这样吗?

var ndays = Math.floor(sec/86400);
var nhours = Math.floor((sec%86400)/3600);
var nminutes = Math.floor(((sec%86400)%3600)/60);
var nseconds = ((sec%86400)%3600)%60;
return ndays + " days " + nhours + " hours " + nminutes + " minutes " + nseconds + " seconds";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保我的Ajax调用始终成功? - How do I make sure my ajax calls are always successful? 如何测试查看我网站的用户是否看不到某些内容,以及如何确保我的测试有效? - How can I test if a user viewing my website can not see some content, and how can I make sure my test works? 在将数据推送到数据库之前,如何确保 if 函数运行? - How do I make sure my if funtion runs before my data is pushed to a database? 如何确保我的 threejs 网格仅在 x 上跟随我的鼠标? - How do I make sure my threejs mesh follows my mouse on the x only? 如何使我的 function 进入迭代? - How do I make my function into an iteration? 如何使我的 chrome 扩展程序正常工作? - How do I make my chrome extension so that it works correct? 我如何确保我的变量在setInterval内部不变 - How can I make sure that my variable does not change inside the setInterval 如何确保我的click函数只用Jquery执行一次? - How can I make sure that my click function executes one time only with Jquery? 我如何重构我的 JS function 以确保此 console.log 结构有效? - How could I refactor my JS function to make sure this console.log structure does work? 如何确保使用React的第一个渲染具有localStorage的值? - How do I make sure my first render with React has a value from localStorage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM