function onbtntime(sec) {
var hours;
var minutes;
var day;
var sec;
if (isNaN(sec)) {
alert("please enter a number");
sec = 0;
}
if (Number(sec) <= 0) {
alert("Please enter a positive number");
sec = 0;
} else if (sec == undefined) {
sec = 0;
} else if (sec >= 60) {
minutes = Math.floor(sec / 60);
sec = Math.round(sec % 60);
if (minutes >= 60) {
hours = Math.floor(minutes / 60);
minutes = Math.round(minutes % 60);
if (hours >= 24) {
day = Math.floor(hours / 24);
hours = Math.round(hours % 24);
}
}
}
if (hours == undefined) {
hours = 0;
}
if (minutes == undefined) {
minutes = 0;
}
if (day == undefined) {
day = 0;
}
id_p.innerHTML = "Time- " + "Day/s: " + day + " ,hours: " + hours + " ,minutes: " + minutes + " ,sec: " +
sec;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h2>Welcome to my Time calculator </h2>
<p id="id_p">Change Second to day/s, hours and minutes.</p>
<label>Enter your sec</label>
<input type="text" id="id_input" />
<button type="button" id="id_button" onclick="onbtntime(parseInt(id_input.value))">שלח</button>
</body>
</html>
这是我的代码,我希望当用户在第一个输入值之后添加一千个输入值时,他们会计算出正确的时间。嗯,例如,我只是希望在发送输入值500之后,输出为: (Time-Day / s:0,Hours:0,minutes:8,sec:20) 如果发送输入值500,则输出为: (Time-Day / s:0,Hours:0,minutes:16,sec:40) 等等... 对不起,我的英语不好 感谢您的帮助
答案 0 :(得分:0)
在这种情况下,您只需要将秒存储在全局变量中即可。
let secs = 0;
function onbtntime(sec) {
if (!Number(sec) || sec <= 0) {
alert("Please enter a number greater than zero!");
return;
}
let
hours = 0,
minutes = 0,
day = 0;
secs += sec;
minutes = Math.trunc(secs / 60);
sec = secs % 60;
if (minutes >= 60) {
hours = Math.trunc(minutes / 60);
minutes = minutes % 60;
if (hours >= 24) {
day = Math.trunc(hours / 24);
hours = hours % 24;
}
}
console.log("Time- " + "Day/s: " + day + ", Hours: " + hours + ", minutes: " + minutes + ", sec: " + sec);
}
//onbtntime("u");
//onbtntime(0);
onbtntime(3600);
onbtntime(86400);
onbtntime(63);
onbtntime(90116);
onbtntime(3);
答案 1 :(得分:0)
只需为全局数字添加全局变量(我将对象称为result),并在每次用户按下按钮时为其分配值和新的输入值即可。
var result = {
hours: 0,
min: 0,
days: 0,
sec: 0
};
function onbtntime(sec) {
var hours;
var minutes;
var day;
var sec;
if (isNaN(sec)) {
alert("please enter a number");
sec = 0;
}
if (Number(sec) <= 0) {
alert("Please enter a positive number");
sec = 0;
} else if (sec == undefined) {
sec = 0;
} else if (sec >= 60) {
minutes = Math.floor(sec / 60);
sec = Math.round(sec % 60);
if (minutes >= 60) {
hours = Math.floor(minutes / 60);
minutes = Math.round(minutes % 60);
if (hours >= 24) {
day = Math.floor(hours / 24);
hours = Math.round(hours % 24);
}
}
}
if (hours == undefined) {
hours = 0;
}
if (minutes == undefined) {
minutes = 0;
}
if (day == undefined) {
day = 0;
}
result.hours += hours;
result.min += minutes;
result.days += day;
result.sec += sec;
id_p.innerHTML = "Time- " + "Day/s: " + result.days + " ,hours: " + result.hours + " ,minutes: " + result.min + " ,sec: " +
result.sec;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h2>Welcome to my Time calculator </h2>
<p id="id_p">Change Second to day/s, hours and minutes.</p>
<label>Enter your sec</label>
<input type="text" id="id_input" />
<button type="button" id="id_button" onclick="onbtntime(parseInt(id_input.value))">שלח</button>
</body>
</html>